phpwordpresswoocommercedecimal

Custom stylization of decimals breaks default WooCommerce calculations


I'm using Stylization of decimals as uppercase in WooCommerce frontend only answer code from my previous question.

Now I have a particular issue: I've noticed that my code is breaking the default WooCommerce decimals calculation rules, is there anything you can see in my code that would be breaking it?

As an example, with this code, a price entered before tax in the backend as 7,39669€ (which is the result of 8.95€/1.21) is wrongly being shown as 8.94€ instead of 8.95€. When I disable this custom code, WooCommerce rules work fine again.

I'd like to ensure that the code "calls" the default WooCommerce calculations, so that the code only changes the display of the decimals and that's all.


Solution

  • To avoid this problem, here I am using a different way to separate decimals from the price:

    add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
    function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
        // Not on backend
        if ( ! is_admin() ) {
            $price_data = explode($decimal_separator, $formatted_price);
            return $price_data[0] . '<sup>' . $price_data[1] . '</sup>';
        }
        return $formatted_price;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.