skip to Main Content

I have several categories and I’m trying to make it so that some categories cannot be added to the cart along with others. (when adding, there should be a redirect to the product and an error message is displayed).

I found the code that helped me here, but I couldn’t edit it correctly so that the rule would apply to other categories

The problem is this
The categories below can be added to the cart among themselves
cheese
cheese-sets
bread
bakery
milk
Whereas the following categories should not be mixed with the categories above and among themselves
glamp
horse
foodtruck
other
farm

original code from link

<?php
//*** Prevent mixture of paint and other prods in same cart ***//
function dont_add_paint_to_cart_containing_other($validation, $product_id) {

// Set flag false until we find a product in cat paint
    $cart_has_paint = false;

// Set $cat_check true if a cart item is in paint cat
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

        $product = $cart_item['data'];

        if (has_term('paint', 'product_cat', $product->id)) {
            $cart_has_paint = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_is_paint = false;
    if (has_term('paint', 'product_cat', $product_id)) {
        $product_is_paint = true;
    }

// Return true if cart empty
    if (!WC()->cart->get_cart_contents_count() == 0) {
        // If cart contains paint and product to be added is not paint, display error message and return false.
        if ($cart_has_paint && !$product_is_paint) {
            wc_add_notice('Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
        // If cart contains a product that is not paint and product to be added is paint, display error message and return false.
        elseif (!$cart_has_paint && $product_is_paint) {
            wc_add_notice('Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
    }
    // Otherwise, return true.
    return $validation;
}

add_filter('woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other', 10, 2);
?>

And reworked but not worked ^)
When "allowed" category in cart and i add any "prohibited" categoru its worked
But when i try add product from glamp and horse not worked? both product goes to cart andd rule not work

<?php
//*** Prevent mixture of certain categories in same cart ***//
function dont_add_categories_to_cart_containing_other($validation, $product_id) {
    // Set flag false until we find a product in prohibited categories
    $cart_has_prohibited = false;
    // Prohibited categories
    $prohibited_categories = array(
        'glamp',
        'horse',
        'foodtruck',
        'other',
        'farm'
    );
    // Allowed categories
    $allowed_categories = array(
        'cheese',
        'cheese-sets',
        'bread',
        'bakery',
        'milk'
    );

    // Set $cat_check true if a cart item is in prohibited categories
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];
        $product_categories = wp_get_post_terms($product->id, 'product_cat', array("fields" => "slugs"));
        $intersect_categories = array_intersect($prohibited_categories, $product_categories);

        if (!empty($intersect_categories)) {
            $cart_has_prohibited = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_categories = wp_get_post_terms($product_id, 'product_cat', array("fields" => "slugs"));
    $intersect_categories = array_intersect($prohibited_categories, $product_categories);
    $product_is_prohibited = !empty($intersect_categories);

    // Return true if cart empty
    if (!WC()->cart->is_empty()) {
        // If cart contains prohibited categories and product to be added is not prohibited, display error message and return false.
        if ($cart_has_prohibited && !$product_is_prohibited) {
            wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please checkout your current cart or empty your cart and try again.', 'error');
            $validation = false;
        }
        // If cart contains a product that is not prohibited and product to be added is from the prohibited categories, display error message and return false.
        elseif (!$cart_has_prohibited && $product_is_prohibited) {
            wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please checkout your current cart or empty your cart and try again.', 'error');
            $validation = false;
        }
    }
    // Otherwise, return true.
    return $validation;
}

add_filter('woocommerce_add_to_cart_validation', 'dont_add_categories_to_cart_containing_other', 10, 2);
?>

Help solve the problem

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    Bhautik thank you for your help your questions and your help motivated me more than ever. I found another option that eventually works for me.

    <?php
        function custom_add_to_cart_validation( $passed, $product_id, $quantity ) {
            // Get the product categories
            $product_categories = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'slugs' ) );
        
            // Creating a list of prohibited combinations of categories
            $prohibited_combinations = array(
                array( 'category-c', 'category-a' ),
                array( 'category-c', 'category-b' ),
                array( 'category-d', 'category-a' ),
                array( 'category-d', 'category-b' ),
            );
        
            // Check whether the selected product is in the basket
            if ( WC()->cart->get_cart_contents_count() > 0 ) {
                foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                    $cart_item_product_id = $cart_item['product_id'];
                    $cart_item_product_categories = wp_get_post_terms( $cart_item_product_id, 'product_cat', array( 'fields' => 'slugs' ) );
        
                    // Checking prohibited combinations
                    foreach ( $prohibited_combinations as $combination ) {
                        if ( in_array( $combination[0], $product_categories ) && in_array( $combination[1], $cart_item_product_categories ) ) {
                            // If a forbidden combination is found, we output an error message
                            wc_add_notice( 'You cannot add products from these categories together to the cart.', 'error' );
                            return false;
                        }
                    }
                }
            }
            
            return $passed;
        }
        add_filter( 'woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 10, 3 );
    ?>    
    

  2. Based on the above discussion. I have added logic. try the below code.

    //*** Prevent mixture of certain categories in same cart ***//
    function dont_add_categories_to_cart_containing_other($passed_validation, $product_id) {
        // Get the product categories
        $product_categories = wp_get_post_terms($product_id, 'product_cat', array("fields" => "slugs"));
    
        // Prohibited categories
        $prohibited_categories = array(
            'glamp',
            'horse',
            'foodtruck',
            'other',
            'farm'
        );
    
        // Allowed categories
        $allowed_categories = array(
            'cheese',
            'cheese-sets',
            'bread',
            'bakery',
            'milk'
        );
    
        // Check if the product is in a prohibited category
        $product_is_prohibited = !empty(array_intersect($prohibited_categories, $product_categories));
    
        // Check if the product is in an allowed category
        $product_is_allowed = !empty(array_intersect($allowed_categories, $product_categories));
    
        // Check if there are any prohibited products in the cart
        $cart_has_prohibited = false;
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            $cart_product_id = $cart_item['product_id'];
            $cart_product_categories = wp_get_post_terms($cart_product_id, 'product_cat', array("fields" => "slugs"));
    
            if (!empty(array_intersect($prohibited_categories, $cart_product_categories))) {
                $cart_has_prohibited = true;
                break;
            }
        }
    
        // Check if the cart has any allowed products
        $cart_has_allowed = false;
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            $cart_product_id = $cart_item['product_id'];
            $cart_product_categories = wp_get_post_terms($cart_product_id, 'product_cat', array("fields" => "slugs"));
    
            if (!empty(array_intersect($allowed_categories, $cart_product_categories))) {
                $cart_has_allowed = true;
                break;
            }
        }
    
        // Return true if cart is empty
        if (WC()->cart->is_empty()) {
            return $passed_validation;
        }
        
    
        // If cart contains prohibited products and the product to be added is not prohibited, display error message and return false.
        if ($cart_has_prohibited && !$product_is_prohibited) {
            wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please review your current cart or empty your cart and try again.', 'error');
            return false;
        }
    
        // If cart contains prohibited products and the product to be added is prohibited, display error message and return false.
        if ($cart_has_prohibited && $product_is_prohibited) {
            wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please review your current cart or empty your cart and try again.', 'error');
            return false;
        }
    
        // If cart contains an allowed product and the product to be added is prohibited, display error message and return false.
        if ($cart_has_allowed && $product_is_prohibited) {
            wc_add_notice('Sorry, you can only purchase one product from the prohibited categories. Please review your current cart or empty your cart and try again.', 'error');
            return false;
        }
    
        /*// If cart contains an allowed product and the product to be added is also allowed, display error message and return false.
        if ($cart_has_allowed && $product_is_allowed) {
            wc_add_notice('Sorry, you can only purchase one product from each category. Please review your current cart or empty your cart and try again.', 'error');
            return false;
        }*/
    
        return $passed_validation;
    }
    
    add_filter('woocommerce_add_to_cart_validation', 'dont_add_categories_to_cart_containing_other', 10, 2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search