questions

Disallow WooCommerce checkout only when items from 2 categories are in cart

Rate this post

I have 3 categories “other”, “category D” and “category T” :

• If the user has in his basket products belonging to the 3 categories, the payment is accepted.

• If the user has in his basket products belonging to “category D” and to “other”, payment is accepted.

• If the user only has products from one category, payment is accepted

• If the user has in his basket products belonging to “category T” and “category D”, the payment is refused.

• If the basket is empty, no error message should appear.

To summarize, payment should only be refused if the user has “category D” and “category T” products in his basket.

Here is my code :

function verifierproduitpanier(){
    $cat_is_catD = false;
    
    foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item){
        $product = $cart_item['data'];

        if (has_term('categorieD', 'product_cat',$product->id)) {
            $cat_is_catD = true;
        }

        $cat_is_catT = false;
        
        if (has_term('categorieT', 'product_cat',$product->id)) {
            $cat_is_catT = true;
        }
        
        $cat_is_other = false;

        if (has_term('autre', 'product_cat',$product->id)) {
            $cat_is_other  = true;
        }
    }

    if ($cat_is_catD && $cat_is_catT && !$cart_is_other){
        wc_add_notice(sprintf('<p>Les produits sélectionnés ne sont pas disponibles</p>'), 'error');
    }
}
add_action( 'woocommerce_check_cart_items', 'verifierproduitpanier' );

When I have items belonging to “Category D” and “Category T” I get an error message that pops up but the problem is when I remove a product from “Category D” and I click on the “cancel” button the error message no longer appears and the payment is accepted.

Any help?

 

✔️Solution:

Try the following revisited code instead, that will only not allow purchases if there are only items belonging to category D and T:

add_action( 'woocommerce_check_cart_items', 'check_products_in_cart' );
function check_products_in_cart(){
    $taxonomy = 'product_cat'; // Product category taxonomy
    $has_cat_d = $has_cat_t = $has_cat_o = false; // Initializing

    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item){
        if ( has_term('categorieD', $taxonomy, $cart_item['product_id'] ) ) {
            $has_cat_d = true;
        }
        elseif ( has_term('categorieT', $taxonomy, $cart_item['product_id'] ) ) {
            $has_cat_t = true;
        }
        elseif ( has_term('autre', $taxonomy, $cart_item['product_id'] ) ) {
            $has_cat_o  = true;
        }
    }

    if ( $has_cat_d && $has_cat_t && ! $has_cat_o ){
        wc_add_notice( __("The Selected products are not available.", "woocommerce"), 'error');
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Leave a Reply

Your email address will not be published.

Back to top button