Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

php - Disable other product categories for a cart item from specific category in Woocommerce

I'm developing a webshop, with different aspects, the first is a regular shop, and the second is a night beer service. i've searched but can't find what i am looking for;

First the night beer service is a specific category that shouldn't be ordered with the regular items (if category 'beerservice' is in the cart, disable all other categories to be added to the cart).

This option also needs to work vice-versa (the other wat around), so if a regular item is added to the cart, the category of 'beerservice' should be disabled to add to the cart.

I am using this answer code that I have found:
Disable shopping when an item from a specific product category is in cart in Woocommerce

That does the job partially, but it's only impossible to add multiple products from the same category, and is doesn't work vice-vera. I have replaced in this code

I also would like to add a time specific message like, 'Order ordered past TIME won't be delivered'(didn't research this) but if it isn't to much trouble.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code will check for parent product categories, so for a defined product category, it will:

  • Avoid adding to cart other product categories when the defined category is in cart.
  • It will remove cart items from other product categories when a product from the defined category is added to cart.

Improved code:

// Custom conditional function that checks for parent product categories
function has_parent_term( $product_id ) {
    // HERE set your targeted product category SLUG
    $category_slug = 'beerservice'; //  <====  <====  <====  <====  <====  <====  <====

    // Convert category term slug to term id
    $category_id   = get_term_by('slug', $category_slug, 'product_cat')->term_id;
    $parent_term_ids = array(); // Initializing

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
        } else {
            $parent_term_ids[] = $term->term_id;
        }
    }
    return in_array( $category_id, $parent_term_ids );
}

// Avoid add to cart others product categories when "beerservice" is in cart
add_filter( 'woocommerce_add_to_cart_validation', 'specific_category_avoid_add_to_cart_others', 20, 3 );
function specific_category_avoid_add_to_cart_others( $passed, $product_id, $quantity) {
    if( WC()->cart->is_empty() || has_parent_term( $product_id ) ) {
        return $passed;
    }

    foreach( WC()->cart->get_cart() as $cart_item ){
        if( has_parent_term( $cart_item['product_id'] ) ) {
            wc_add_notice( __('Alert message 1 (avoid add to cart)', 'woocommerce' ), 'error' ); // Display a custom error notice
            return false; // Avoid add to cart
        }
    }
    return $passed;
}

// Remove other items when our specific product is added to cart
add_action( 'woocommerce_add_to_cart', 'conditionally_remove_other_products', 20, 4 );
function conditionally_remove_other_products ( $cart_item_key, $product_id, $quantity, $variation_id ){
    if( has_parent_term( $product_id ) ) {
        foreach( WC()->cart->get_cart() as $item_key => $cart_item ){
            if( ! has_parent_term( $cart_item['product_id'] ) ) {
                WC()->cart->remove_cart_item( $item_key );
                wc_add_notice( __('Alert message 2 (Item removed from cart)', 'woocommerce' ), 'error' ); // Display a custom error notice
            }
        }
    }
}

Code goes in function.php file of your active child theme (active theme). Tested and works.


Related: Disable shopping when an item from a specific product category is in cart in Woocommerce


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...