Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mahdi-alavi/1be04ad4247c7f0e4afb51284c627dfc to your computer and use it in GitHub Desktop.
Save mahdi-alavi/1be04ad4247c7f0e4afb51284c627dfc to your computer and use it in GitHub Desktop.
Woocommerce: limit customer to only one category per transaction
// original source http://stackoverflow.com/a/35072168
function itl_is_product_the_same_cat( $valid, $product_id, $quantity ) {
global $woocommerce;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' ); // get the current items
foreach ( $terms as $term ) {
$cat_ids[] = $term->term_id; // get all the item categories in the cart
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id; // get all the categories of the product
}
}
if ( ! empty ( WC()->cart->get_cart() ) && $valid ) {
$same_cat = array_intersect( $cat_ids, $target_cat_ids ); // check if they have the same category
if ( count( $same_cat ) == 1 ) {
wc_add_notice( 'You can only have 1 item per category in your cart', 'error' );
return false;
} else {
return $valid;
}
} else {
return $valid;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'itl_is_product_the_same_cat', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment