Support

Account

Home Forums General Issues Automatic Calculation price per kilo

Solved

Automatic Calculation price per kilo

• Hi,

I’m new with ACF and I’m amazed about all the time I could have saved if I knew this plugin before.

I’ve been looking for a solution to calculate the price per kg of my product automatically. In my country, it is mandatory to show the price per kilo under every food product or drink.
The formula on paper is easy:

1000g * A (price of the product) / B (weight of the product).

So an apple that would weight 200 grams and cost 5β¬, the price per kilo would be:
1000 * 5 / 200 = 25β¬/kilo.

I was wondering if there is a way to calculate this using an ACF field. Is it possible to use the price field and the weight field of WooCommerce to calculate in another custom field the price per kilo ? That would be perfect for me, and I’ve been wasting so much time for this simple thing …

• Hi Nayrod!

You can do this with the update_field function.

update_field(\$selector, \$value, [\$post_id]);

\$selector is your price/per kilo field name
\$value is calculated price
\$post_id is current product ID

You can use a function in your function.php file to calculate the price (\$value).

• Hi Celestial,

I’ve been trying to understand how to create a function using this, but I’m brand new with PHP and I don’t really understand how to use it. I can study it of course but if you have the formula, it would save me a lot of time before I can reach this level.

Thank you very much !

• Hi!

For example

1. Create an ACF field (price_per_kilo) for Product.

2. In Woo template use something like this

``````
global \$product;

\$price = \$product->get_price();

\$weight = \$product->get_weight();

\$id = \$product->get_id();

\$value = 1000 * \$price / \$weight;

update_field('price_per_kilo', \$value, \$id); // this code updates data for this field

the_field('price_per_kilo', \$product->\$id ); //show price per kilo

``````
• Hi Celestial and thank you for your precious help

I tried the code you’ve given to me by creating a file in /wp-content/plugins/woocommerce/templates

Nothing happens in my product page in my dashboard.

I also tried to copy paste this code in Rich Snippets but it tells me there is an error line 3 so I presume it’s not a snippet that I can use with this plugin.

I might do something wrong though …

• Hi, Nayrod!

Have you already created the ‘price_per_kilo’ field in the ACF fields for products?

If you change files in the plugin folder itself, then after the update all edits will disappear. So you need a new folder in your theme folder.

You need to create a new folder in your theme. Like this directory

/ wp-content / themes / your-child-theme / woocommerce / single-product /

Copy the price.php template from the / wp-content / plugins / woocommerce / templates / single-product directory to your new folder (/ wp-content / themes / your-child-theme / woocommerce / single-product /)

Open the price.php template and paste my code (don’t duplicate the global \$product).

If the code doesn’t work try this. Πpen the product in the admin panel. An empty “price_per_kilo” field that you created at the beginning should appear. Make sure the price and weight (in delivery) are specified. Save the product. Go to the front-end of the product page, the code should work.

If there are any mistakes, write down which ones. Write down, step by step, what you are doing and in which files.

It works in my local project. Good luck!

• Thank you very much ! It worked π

The last line isn’t even necessary (the_field(‘price_per_kilo’, \$product->\$id ); //show price per kilo) as I put a shortcode below the price to put a prefix and a sufix with some CSS customization. It even works when the price is on sale !

The only thing is that the price calculated doesn’t appear on the back office in the price per kilo box but it’s no big deal as the most important thing is that it’s shown on the product page.

Thank you again Celestial, I owe you a pint !

Best regards

• You’re welcome! π

The price per kilogram will be shown in the backend after viewing the product in the store. This is due to the field updating only after the update field function is launched, which will be launched during the loading of the product page.
If you need to automatically calculate the price per kg in the admin panel, then you can use JS. This is a forum for ACF fields, so I wrote how to solve your problem with it.
If you do it in the backend, you just write a script for calculating the price. You specify where to get the weight, where to get the price and where to insert the result.

• You might want to have a look at the Calculated Fields for ACF plugin that most likely will solve this issue for you without writing any PHP code.

https://wordpress.org/plugins/calculated-fields-for-acf/

• Does someone know how to limit to two decimal places ? I have like 10 digits and I don’t finda ny solution to make it look like a price per kg (for example 12,54β¬ and not 12,54966258842β¬)

• Ok, found, the code is:

global \$product;

\$price = \$product->get_price();

\$weight = \$product->get_weight();

\$id = \$product->get_id();

\$value = number_format( 1000 * \$price / \$weight, 2 );

update_field(‘price_per_kilo’, \$value, \$id); // this code updates data for this field

the_field(‘price_per_kilo’, \$product->\$id ); //show price per kilo

• This works, but how can I add tax 20% to the kilo price?
\$value = number_format( 1 * \$price / \$weight, 2 );

• Calculate the new price including tax

\$new_price = \$price * (1 + \$tax_rate / 100);

OR change the price

\$price *= 1 + \$tax_rate / 100; // in your case 1 + 20/100 = 1.2

Taxes may vary from product to product. You can get actual tax rate from Woo.

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.