// 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 );