Last active
September 27, 2024 04:05
-
-
Save asifulmamun/8b0afe12f58ea4faff1e2ebad2056f63 to your computer and use it in GitHub Desktop.
WooCommerce WP - minimum weight specify while order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WooCommerce WP - minimum weight specify while order | |
add_action( 'woocommerce_check_cart_items', 'spyr_set_weight_requirements' ); | |
function spyr_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 = 2; | |
// 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 | |
// happens to be less than the minimum required before checking out. | |
// Will display a message along the lines of | |
// A Minimum Weight of 4kg is required before checking out. (Cont. below) | |
// Current cart weight: 12.5kg | |
if( $cart_contents_weight < $minimum_weight ) { | |
// Display our error message | |
wc_add_notice( sprintf('<strong>Warning!</strong>'),'error'); | |
wc_print_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' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment