A WooCommerce fan asked me: “How do you deny checkout if the cart weight is above a certain threshold?“. Well, this is straight forward, so there you go!

WooCommerce: show checkout error if weight exceeds a threshold

PHP Snippet: Deny WooCommerce Checkout Processing if Cart Weight > Threshold


/**
 * @snippet       Deny Checkout Based on Cart Weight - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=21748
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */ 

add_action( 'woocommerce_after_checkout_validation', 'bbloomer_deny_checkout_if_weight' );

function bbloomer_deny_checkout_if_weight( $posted ) {
$max_weight = 100;
if ( WC()->cart->cart_contents_weight > $max_weight ) {
   $notice = 'Sorry, your cart has exceeded the maximum allowed weight of ' . $max_weight . get_option( 'woocommerce_weight_unit' );
   wc_add_notice( $notice, 'error' );
}
}

Share:

Leave a Reply