Home » WooCommerce: Deny Checkout Based on Cart Weight

WooCommerce: Deny Checkout Based on Cart Weight

by Tutor Aspire

A WooCommerce fan asked me: “How do you deny checkout if the cart weight is above a certain threshold?“.

Well, this is straight forward thanks to a WooCommerce core function called “cart_contents_weight” which allows you to get the total weight of your cart, and then the “wc_add_notice” function which shows a notification error.

Enjoy!

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */ 

add_action( 'woocommerce_after_checkout_validation', 'tutoraspire_deny_checkout_if_weight' );

function tutoraspire_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' );
   }
}

You may also like