A simple, powerful and independent e-commerce platform.
Sell anything with ease.

Deny checkout based on cart weight

If you want to restrict orders based on a maximum or minimum order weight, you can do so with the following code. This function displays an error message for total cart weights above 100 in three places – on the cart page, on the checkout page and when an order is initiated. The order will not be processed until the cart has been revised to bring the weight below the maximum.

If you are setting these limits to an order it is also a good idea to display the total weight on cart and checkout pages.

		/**
 * Deny checkout based on cart weight
 */

add_action('woocommerce_before_cart', 'cc_deny_checkout_by_weight', 99);
add_action('woocommerce_before_checkout_form', 'cc_deny_checkout_by_weight', 99);
add_action('woocommerce_after_checkout_validation', 'cc_deny_checkout_by_weight', 99);

function cc_deny_checkout_by_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');
    if( is_cart() ) {
      wc_print_notice($notice, 'error');
    } else {
      wc_add_notice($notice, 'error');
    }
  }
}
	
View Raw Code ID: 9985