How to calculate values with math formulas
Price of a product decides how much your income from your WooCommerce site will be. When scraping products from other e-commerce sites, you might want to change the prices by applying a mathematical formula to it, such as increasing or decreasing it by 5%. This guide walks you through how you can do that.
WP Content Crawler provides two ways to calculate a value. One way is to use the filters. We will take a look at the other way in this post, which is by using the options box. We are going to crawl this eBay product as an example. You can copy the link below:
1
https://www.ebay.com/itm/114324407723
If this product does not exist at the time of reading, you can browse eBay (or another e-commerce site) to find another product and use that product's link instead.
1. Configure the settings to crawl a product
Create a new site by clicking to "Content Crawler > Add New" link from the admin panel's sidebar,
write eBay
as the name of the site, and enter https://www.ebay.com/
as the Site URL
.
2. Configure the category URLs setting
Go to the Category tab and enter / to the Category URLs setting. We will not retrieve the product URLs from the category pages in this guide. Because this setting is required to be filled, we trick the plugin by entering a dummy value to the setting.
3. Enable WooCommerce options
Go to the Main tab and enable the "Use custom general settings" setting. This will allow us to change the post type just for this site settings.
Now, go to the Settings tab and click the "Load general settings" button. This automatically fills the settings from the existing general settings.
Then, go to the "Post type" setting and select "product". This will enable the WooCommerce options.
Now, save the settings and make sure the page is refreshed.
After the page is refreshed, the WooCommerce options will be available under the Post
tab.
4. Define a CSS selector that finds the product price
Go to the Post tab > WooCommerce Section > Regular price selectors setting and then open its visual inspector by clicking to the button with the wrench icon.
In the visual inspector, click the price so that its CSS selector is found. Then, click the "use" button to use the found CSS selector.
5. Prepare the price
Click the magnifier icon of the CSS selector to test it. The result shows US $47.95
.
This value contains non-numeric values. To be able to use the value in a mathematical formula, we need to remove
the non-numeric characters from the price.
Open the options box of the CSS selector so that we can remove the non-numeric characters.
In the options box, go to the "Find-Replace" tab, enter "US $" to the "find" input, and leave the "replace" input empty. After that, click the magnifier button to test the replacement rule. This tells the plugin to find "US $" and replace it with nothing, resulting in the "US $" text to be removed. You can see that we removed the non-numeric characters from the price. The price is converted from "US $47.95" to "47.95". If there are characters other than numbers and "." in your price text, you should remove them all. A price value can contain only "." and numbers for it to be considered as a number. This is crucial, because you cannot apply a mathematical formula if the price is not a number. If the decimal separator in your price value is something other than a dot character, you should add a find-replace rule that replaces it with a dot character.
6. Change the price with a mathematical formula
In the options box, go to the "Calculations" tab, enter "2" for the precision,
enter "x * 1.05" as a formula, and click the magnifier button to
test your formula. Here, x
is replaced with our price, which is 47.95
. *
character is used as a
multiplication operator. Multiplying the price with 1.05
results in 5% increase in the price. When we multiply
47.95
with 1.05
, we get 50.3475
. This value has 4 digits after its decimal separator, meaning that its
precision is 4
. Because we entered 2
as the precision, the plugin rounds the result to have 2 digits after
the decimal separator, which results in 50.35
.
You can use +
(addition), -
(extraction), *
(multiplication), and /
(division) operators. Also, you can
use ^
(exponentiation). For example, to calculate the square of a value, you can use x^2
formula. You can
use x
in multiple places. For example, you can rewrite x * 1.05
as x + 0.05x
. This will result in the same
value, 50.35
.
Close the options box by clicking outside and click the magnifier button of the CSS selector to test the configuration. As you can see, the find-replace and calculation rules we defined in the options box are applied. We successfully increased the price by 5%.
Now, you can configure the rest of the site settings.
Conclusion
In this guide, we learned how to increase the price of an e-commerce product that is crawled via the plugin. Although
this guide works with a price, it is possible to calculate any numeric value. The options box is available for almost
any CSS selector option. You just need to open the options box, prepare the value to be numeric by defining find-replace
rules, and then define your formula in the Calculations
tab.
Additionally, you can enter multiple formulas to the Formulas
setting. When you do that, the plugin chooses a random
formula from the defined formulas and applies it. When doing that, keep in mind that, if you enable recrawling, the
plugin will probably apply a different formula, resulting in changes in the price on almost every recrawl.
Thanks for reading!