Search code examples
phpwordpresswoocommercecartvolume

Display a notice if shipping volume exceed a defined value in Woocommerce


I have some php code in a widget in the sidebar that displays the total m3 volume of their order. As the customer adds items the m3 value increaes.

I want to alert the client when the volume of the order reaches 68m3 which is equvalent to a 40 foot container. Is there a way to take the value from my snippet and display a notification box?

The snippet displaying the volume of the order is using code in this answer Displaying total sum of shipping Volume of Woocommerce order on page

Thx in advance is anyone knows how to achieve this.


Solution

  • I have answered in your linked question thread with a custom function that calculate the cart volume based on the measurement unit set in Woocommerce.

    I use that get_cart_volume() function in the code below to display a custom notice this way:

    add_action( 'template_redirect', 'display_volume_alert' );
    function display_volume_alert(){
        if( get_cart_volume() >= 68 )
            wc_add_notice( __("Your order total volume has reached more than <strong>68 m3</strong>", "woocommerce"), 'notice' );
    }
    

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

    enter image description here