Home > Enterprise >  WooCommerce Cart Taxonomy Limit
WooCommerce Cart Taxonomy Limit

Time:11-30

I have created a custom taxonomy (slug: restaurant) for WooCommerce products where I am currently using to filter products. I need to limit the number of Restaurants that can be added to the cart at once. The following code limits the number of "products" rather than the "taxonomy". Any help is appreciated.

public function CheckRestaurantInCart($product_id){
            if( WC()->cart->is_empty() )
                return true;

            foreach ( WC()->cart->get_cart() as $cart_item ) {
                $ci[] = $cart_item['data']->get_category_ids();
            }

            $terms = get_the_terms ( $product_id, 'restaurant' );

            $cat_output = array_unique($ci, SORT_REGULAR);
            $cat_count = count($cat_output);
            $allowed_count = 2;

            //check whether the cart has the same restaurant and ignore
            if (in_array_r($terms[0]->term_id, $cat_output))
                return true;

            if ($cat_count >= $allowed_count) {
                wc_add_notice( __('Restaurant count allowed is: '. $allowed_count .' at this time'), 'error' );
                return false;
            }
            return true;

        }

enter image description here

  • Related