Skip to content

Instantly share code, notes, and snippets.

@rajeshsingh520
Created June 23, 2025 09:48
Show Gist options
  • Save rajeshsingh520/383f085276d6476089486e6b0c9a4b18 to your computer and use it in GitHub Desktop.
Save rajeshsingh520/383f085276d6476089486e6b0c9a4b18 to your computer and use it in GitHub Desktop.
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