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 …
Thanks in advance !
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).
Thank you for your reply 🙂
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 !
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 …
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 !
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.
Ok, found, the code is:
$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
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We've just released ACF 5.12.3 with a security fix to prevent arbitrary file uploads to forms with ACF fields.— Advanced Custom Fields (@wp_acf) July 14, 2022
Now that we've released this update, we recommend updating your sites as soon as possible.
© 2022 Advanced Custom Fields.