Support

Account

Home Forums General Issues Calculate a field using price and acf value

Solved

Calculate a field using price and acf value

  • For my pharmacy store I have product which contains 10 tablets in a strip. The default price is the price of the whole strip. I want to calculate price per tablet.

    I have added the following custom fields:
    Number of tablets:
    Price per tablet:

    I want to calculate the price per tablet using “(regular price or sale price)/(number of tablets)”.

    I want to use code-snippets instead of child theme.

  • Update:
    I was able to update the price per tablet by using the following code:

    function wc_update_price_per_unit($post_id) {
        $value= get_field('_price')/get_field('number_of_unit');//use _regular_price to get regular woocommerce price
    	$field_name = "price_per_unit";
    	update_field($field_name,$value,$post_id);
    }
    add_action('save_post','wc_update_price_per_unit');

    The price_per_tablet value gets populated when saving the post. How can I see changes in the price_per_unit field before saving or updating the post.

Viewing 2 posts - 1 through 2 (of 2 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.