Maximum Cart Weight
Prevents checkout when the products in the cart weigh more than your carrier allows and tells the customer by how much they are over.
Opens WPIDE on
Code
add_action( 'woocommerce_check_cart_items', function () {
$max_weight = 30; // In the store weight unit (kg, lbs...).
$weight = (float) WC()->cart->get_cart_contents_weight();
if ( $weight > $max_weight ) {
wc_add_notice( sprintf( 'Your order weighs %1$s, above our maximum of %2$s per order. Please remove %3$s to continue.', wc_format_weight( $weight ), wc_format_weight( $max_weight ), wc_format_weight( $weight - $max_weight ) ), 'error' );
}
} );
Notes
Set $max_weight in your store's weight unit. Products without a weight count as 0.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.