• Resolved milosztor

    (@milosztor)


    Hi, I have set 3 decimal points, but I want to display 2 decimal price if the last two numbers are 0, e.g. 15,900 I want to display as 15,90 but 15,000 as 15 or 15,00. How can I do that?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support con

    (@conschneider)

    Engineer

    Hi there,

    Here is a filter explained aimed at similar behaviour that you can try and modify: https://krokedil.com/dont-display-prices-with-0-decimals-in-woocommerce/

    Kind regards,

    Thread Starter milosztor

    (@milosztor)

    I visited that page. I have this code, but not working.

    function remove_zeroes_from_price($price) {
    	//$price = str_replace(',000', ',00', $price);
    	$price = preg_replace('/(?<=\d{2})(0+)$/', '', $price);
    	return $price;
    }
    
    add_filter('wc_price', 'remove_zeroes_from_price');

    If I change $price with e.g. ‘75,000’ function preg_replace() working well but not with the $price. What can I do?

    Commented str_replace() function works on $price.

    • This reply was modified 5 years, 4 months ago by milosztor.
    • This reply was modified 5 years, 4 months ago by milosztor.
    Thread Starter milosztor

    (@milosztor)

    Any ideas?

    Moderator Hari Shanker R

    (@harishanker)

    Hi @milosztor

    This could probably be because the $price attribute is not defined properly. Try adding the global $woocommerce; value within the function to call everything within WooCommerce, including the price.

    The code that you shared does not seem to have priority set to it. It might help to add priority to the actions.

    So the resulting code would look like this:

    function remove_zeroes_from_price($price) {
    	global $woocommerce;
    	$price = preg_replace('/(?<=\d{2})(0+)$/', '', $price);
    	return $price;
    }
    
    add_filter('wc_price', 'remove_zeroes_from_price', 10, 2);

    Now, I’m not a developer and I’m not sure if this would work, but feel free to give it a try.

    Hi @milosztor,

    It’s been a while since we last hear from you so I’m going to mark this topic as resolved. If you still need help, please let me know.

    Best,

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hide last zero when 3 decimal points are set’ is closed to new replies.