skip to Main Content

I’ve seen a couple of similar answers, but not quite what I need. Basically I just want to display a notice to show at checkout when the total order weight is 225kg or over.

Based on Display weight and the remaining weight message on woocommerce cart and checkout answer code, here is my adapted code:

    // Display a custom shipping message when cart weight exeeds a limit
add_filter( 'woocommerce_before_cart', 'display_total_weight_notice' );
add_filter( 'woocommerce_before_checkout_form', 'display_total_weight_notice' );
function display_total_weight_notice( $message ) {
    // DEFINE the allowed weight limit
    $cart_total_weight = WC()->cart->get_cart_contents_weight();

    if( cart_total_weight >= 225 ) :

    wc_print_notice( sprintf(
        __( 'Your order has a total weight of %s. This will be shipped via Pallet Delivery, by continuing you agree to the following:<br><br> 
        > Ground/Road around the property will be flat, level and solid. <br> 
        > Standard Delivery will take place between 9am-5pm and will require a signature. You can advise a safe place to leave your order, however this is at the driver’s discretion to do so, we hold no responsibility if left. <br> 
        > Where a delivery cannot be made due to access, a re-delivery can be made at your request for a charge of £45.00 per pallet on the following day or when arranged. <br> 
        > In any cases of Damages/Shortages you will note this on the delivery note when you Sign for it, and then contact us right away. No refunds/replacements will be given if damages are not noted at delivery. <br> 
        > Pallet Orders cancelled en route, or returned due to ground Conditions/Access Restrictions will incur a return fee of £45 per pallet (subject to location), deducted from any refunds given. <br> 
       >  Orders over 1000kg will require your own forklift. 

' ),
        '<strong>' . wc_format_weight($cart_total_weight) . '</strong>',
    ),'notice' );

    endif;
}

But it’s not working for me.

2

Answers


  1. Chosen as BEST ANSWER

    Managed it with

    // Display a custom shipping message when cart weight exeeds a limit
    add_action( 'woocommerce_check_cart_items', 'checkout_required_min_weight_mood' );
    function checkout_required_min_weight_mood () {
        
        // only on the cart and check out pages
        if( ! ( is_cart() || is_checkout() ) ) return;
    
        // Get the Cart's content total weight
        $total_weight = WC()->cart->get_cart_contents_weight();
    
        // here the minimum weight
        $minimum_weight = 225; // 225 kg
    
        // If the total weight is less than the minimum, we avoid payment and display an error notice
        if( $total_weight >= $minimum_weight  ) {
            // Visualizza un avviso di errore dinamico
            wc_add_notice( sprintf(
                'MESSAGE TO DISPLAY HERE',
                wc_format_weight($total_weight),
                wc_format_weight($minimum_weight)
            ), 'notice' );
        }
    }
    

  2. I just tested your code, and it correctly displays a message on cart page.

    There’s just a typo in your code, your missing the $ before your variable:

    if( $cart_total_weight >= 225 ) :
    

    When setting at least one product with a weight, I get the correct total weight from $cart_total_weight = WC()->cart->get_cart_contents_weight();. If no product in cart as a weight defined, it correctly returns 0.

    If you haven’t edited the cart template, it should work. If you edited it, check that the do_action() is still here.

    enter image description here

    Here I edited the limit to 20kg to display the message, with a product of 25Kg to match the criterias.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search