Search code examples
phpwordpresswoocommercerounding

How can I stop rounding off shipping charge in Woocommerce


When I'm adding Shipping Charge 58.75 to Woocommerce. It's rounding off this to 59. I want exact Shipping Charge 58.75.

Anyone who has faced this?


Solution

  • /**
     * Get rounding precision for internal WC calculations.
     * Will increase the precision of wc_get_price_decimals by 2 decimals, unless WC_ROUNDING_PRECISION is set to a higher number.
     *
     * @since 2.6.3
     * @return int
     */
    function wc_get_rounding_precision() {
        $precision = wc_get_price_decimals() + 2;
        if ( absint( WC_ROUNDING_PRECISION ) > $precision ) {
            $precision = absint( WC_ROUNDING_PRECISION );
        }
        return $precision;
    }
    

    Please check WC_ROUNDING_PRECISION has been set somewhere else ( maybe in theme )

    Here are some constants you can set to alter rounding calculation: https://github.com/woocommerce/woocommerce/blob/master/includes/class-woocommerce.php#L207-L209. They’d be defined earlier in your wp-config file to change I believe.

    Can see here, that WC will mostly use whichever is higher: https://github.com/woocommerce/woocommerce/blob/64bcabf0af9a05274d53ec833a4e8c9153509bc4/includes/wc-core-functions.php#L1549-L1553. Either WC_ROUNDING_PRECISION constant, or the number of decimals you have set plus 2.