Change number of decimals in Woocommerce cart totals

前端 未结 3 639
走了就别回头了
走了就别回头了 2021-01-23 04:46

In Woocommerce, I\'ve set the number of decimal to 7 on in Woocommerce general settings, so I can display the product price like this $0.0453321

3条回答
  •  粉色の甜心
    2021-01-23 05:35

    In my case I needed to keep 3 decimal places "visible" throughout the site but approximate only the grand total to 2 decimal places (with a zero for the third decimal place) because my payment gateway accepted only 2 significant digits after the decimal.

    Prices displayed as: 0.336
    Taxes: 0.017
    But grand total needed to be: 0.350 (instead of 0.353)
    

    I ended up not using the code because it was so horrific but you can say it was a mental excercise:

    add_filter( 'wc_get_price_decimals', 'change_prices_decimals', 20, 1 );
    function change_prices_decimals( $decimals ){
    
        if( is_cart() || is_checkout() )
        {
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,0);
            $length = count($trace);
            for ($i = 0; $i < $length; $i++)
            {
                if($trace[$i]["function"] == "set_total"){
                    $decimals = 2;
                    return $decimals;
                }
            }
        }
    
        return $decimals;
    }
    

提交回复
热议问题