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
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 !
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 …
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 !
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!