Created
June 23, 2025 09:48
-
-
Save rajeshsingh520/383f085276d6476089486e6b0c9a4b18 to your computer and use it in GitHub Desktop.
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
add_action( 'wp_loaded', function () { | |
if ( is_admin() || WC()->cart->is_empty() ) { | |
return; | |
} | |
$category_id = 106; | |
$quantity_in_category = 0; | |
foreach ( WC()->cart->get_cart_contents() as $cart_item ) { | |
$product_id = $cart_item['product_id']; | |
if ( has_term( $category_id, 'product_cat', $product_id ) ) { | |
$quantity_in_category += $cart_item['quantity']; | |
} | |
} | |
if ( $quantity_in_category > 7 ) { | |
wc_add_notice( 'You cannot add more than 7 items from this category.', 'notice' ); // 'notice' will not block the add-to-cart | |
} | |
}, 10, 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment