Created
November 19, 2021 16:35
-
-
Save femiyb/50e13de1d30547d415b32c9223b1381a to your computer and use it in GitHub Desktop.
WooCommerce Minimum Cart Total per category
This file contains 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
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