Search code examples
phpwordpresswoocommercecartorders

Allow only one category per order in WooComerce


I'm trying to restrict customers on my WooCommerce store to only be allowed to order from 1 category at a time.

The code I am trying is just restricting everything for some reason.

function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
// start of the loop that fetches the cart items
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
    }           
}
$same_cat = array_intersect($cat_ids, $target_cat_ids); //check if they have the same category
if(count($same_cat) > 0) return $valid;
else {
    wc_add_notice( 'This product is in another category!', 'error' );
    return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);

I'm not trying to limit it to 1 product per category, I'm trying to restrict them so they can only order products from 1 category per order.

Once they've added a product to their basket from the category "Candy" for example, they then won't be able to add a product from any other category other than "Candy".


Solution

  • Assuming that each product contains only 1 category

    function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
        // If passed
        if ( $passed ) {
            
            // If cart is NOT empty when a product is added     
            if ( !WC()->cart->is_empty() ) {
                
                // Set vars
                $current_product_category_ids = array();
                $in_cart_product_category_ids = array();
                
                // Get current product categories via product_id
                $current_product_category_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
    
                // Loop through cart items checking for product categories
                foreach ( WC()->cart->get_cart() as $cart_item ) {
                    // Get product categories from product in cart via cart item product id
                    $in_cart_product_category_ids = array_merge( $in_cart_product_category_ids, wc_get_product_term_ids( $cart_item['product_id'], 'product_cat' ) );
                }
                
                // Removes duplicate values
                $in_cart_product_category_ids = array_unique( $in_cart_product_category_ids, SORT_NUMERIC );
                
                // Compare
                $compare = array_diff( $current_product_category_ids, $in_cart_product_category_ids );
                
                // Result is NOT empty
                if ( !empty ( $compare ) ) {
                    wc_add_notice( 'This product is in another category!', 'error' );
                    $passed = false;
                }
            }
        }
    
        return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );