Search code examples
phpwordpresswoocommercecartsubtotal

WooCommerce - Rounding total prices before placing order when using 4 decimals prices


So I am currently working on a website that sells cereals and spices in bulk.

The unit for 1 product is 1 gram. I set a minimum order amount of let's say 100 units (100 grams) for a product. But the price for 1 gram can be very low (sometimes $0.0045 , so not even half a cent for 1g).

I also set a minimum to place an order and the cart has to be at least $15 total.

Sometimes a cart's total will be an amount with 4 decimals like $20.5517 . I would like the sub-total and total prices shown in the cart to be rounded up to 2 decimals. But I need to keep the items prices with the 4 decimals because that's the only way I can stay competitive with my prices.¸

Basically I need the back-end to keep the 4 decimals prices and show the 4 decimals on the products too (thats how its already set up) but I want the total to be rounded up before the customer can pay via paypal.

Can anybody help me with that?

Thanks


Solution

  • Here is a solution. But you will have to make yourself all the necesary changes in woocommerce related templates.

    So first if you don't know how to customize WooCommerce templates correctly, read this:
    Template Structure + Overriding Templates via a Theme

    Then now with the custom function below that will do the job on displayed formatted html prices (changing prices from 4 decimals to 2 decimals and keeping html tags arround), you will be able to make the necessary changes on woocommerce related templates:

    function wc_shrink_price( $price_html ){
        // Extract the price (without formatting html code)
        $price = floatval(preg_replace('/[^0-9\.,]+/', '', $price_html));
    
        // Round price with 2 decimals precision
        $shrink_price = round($price, 2); 
    
        // Replace old existing price in the original html structure and return the result
        return str_replace($price, $shrink_price, $price_html);
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and works


    USAGE EXAMPLE:

    On woocommerce template cart/cart_totals.php at line 33 you have this Original code:
    (for subtotal displayed price)

    <td data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>"><?php wc_cart_totals_subtotal_html(); ?></td>
    

    If you search for wc_cart_totals_subtotal_html() function you will see that is using this WC_Cart method: WC()->cart->get_cart_subtotal()
    So you can replace it this way:

    <td data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>">
    <?php 
        // Replacement function by a WC_Cart method
        $subtotal_html_price = WC()->cart->get_cart_subtotal();
    
        // Here we use our custom function to get a formated html price with 2 decimals
        echo wc_shrink_price( $subtotal_html_price );
    ?>
    </td>
    

    So know as you can see you will need to do something similar for all cart prices.
    The cart and checkout templates are in cart and checkout subfolders…
    It's your time to work a bit!