Hello friends, In this article we w’ll learn “How to set Minimum weight for every order in WooCommerce“. Let’s start if we want to set a limit of 30kg for each order. In WooCommerce, there are several ways to set the parameters for WooCommerce purchase limit.
Here we will use the “woocommerce_check_cart_items” action which is provided by WooCommerce to run the functions and execute the code.
Setting a Minimum Weight for WooCommerce Purchase Limit
Many online stores don’t allow their customers to checkout cart untill his purchase product weight does not exceed a pre set limit. Minimum product weight require to reduce delivery costs and the process becomes more streamlined.
You need to place the following code in the function.php, located in theme folder.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' ); function cldws_set_weight_requirements() { // Only run in the Cart or Checkout pages if( is_cart() || is_checkout() ) { global $woocommerce; // Set the minimum weight before checking out $minimum_weight = 30; // Get the Cart's content total weight $cart_contents_weight = WC()->cart->cart_contents_weight; // Compare values and add an error is Cart's total weight if( $cart_contents_weight < $minimum_weight ) { // Display our error message wc_add_notice( sprintf('<strong>A Minimum Weight of %s%s is required before checking out.</strong>' . '<br />Current cart weight: %s%s', $minimum_weight, get_option( 'woocommerce_weight_unit' ), $cart_contents_weight, get_option( 'woocommerce_weight_unit' ), get_permalink( wc_get_page_id( 'shop' ) ) ), 'error' ); } } } |
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co