Skip to content

Instantly share code, notes, and snippets.

@femiyb
Created November 19, 2021 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save femiyb/50e13de1d30547d415b32c9223b1381a to your computer and use it in GitHub Desktop.
Save femiyb/50e13de1d30547d415b32c9223b1381a to your computer and use it in GitHub Desktop.
WooCommerce Minimum Cart Total per category
function cat_cart_count( $cat_name ) {
$cat_count = 0;
// Iterating through each cart item
foreach(WC()->cart->get_cart() as $cart_item)
if( has_term( $cat_name, 'product_cat', $cart_item['product_id']))
$cat_count += $cart_item['quantity'];
return $cat_count;
}
function wc_min_item_required_qty() {
$min_item_qty = 6;
$cat_total = cat_cart_count('ciders');
if ($cat_total){
if($cat_total < $min_item_qty ) {
wc_clear_notices(); // Clear all other notices
// Add an error notice (and avoid checkout).
wc_add_notice( sprintf( 'The minimum order quatity for ciders is 6.', $min_item_qty , $cat_total ), 'error' );
}
}
}
add_action( 'woocommerce_check_cart_items', 'wc_min_item_required_qty' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment