Home Forums General Issues Automatic Calculation price per kilo


Automatic Calculation price per kilo

    • Nayrod

    • November 6, 2020 at 2:55 am


    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 !

  • 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).

    • Nayrod

    • November 9, 2020 at 9:32 pm

    Hi Celestial,

    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 !

  • 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
    • Nayrod

    • November 10, 2020 at 10:54 pm

    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!

    • Nayrod

    • November 12, 2020 at 10:00 pm

    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.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.