Home > database >  If product A is in cart, remove from cart any items in category B, C and D
If product A is in cart, remove from cart any items in category B, C and D

Time:06-17

I've been searching for hours and tried various snippets to achieve what I need but I'm failing miserably.

I need products contained within categories B, C & D removed from the cart if a product in category A is present/added. At this point, I would be happy with just removing products in categories B, C & D if a specific product ID is present/added.

I think the code from this post is close, but I just can't seem to adapt it for my needs.

Any help appreciated. Thank you!

function action_woocommerce_checkout_before_customer_details() {
    // Add categories. Multiple can be added, separated by a comma
    $categories = array( 'cat-a' );
    
    // Initialize
    $cart_item_keys = array();
    $has_category = false;
    
    // WC Cart NOT null
    if ( ! is_null( WC()->cart ) ) {
        // Loop through cart items
        foreach ( WC()->cart->get_cart_contents() as $cart_item_key => $cart_item ) {
            // Get product id
            $product_id = $cart_item['product_id'];
            
            // NOT certain category     
            if ( ! has_term( $categories, 'product_cat', $product_id ) ) {
                // Push to array
                $cart_item_keys[] = $cart_item_key;
            } else {
                $has_category = true;
            }
        }
        
        // NOT empty & has category is true
        if ( ! empty ( $cart_item_keys ) && $has_category ) {
            // Loop through all cart item keys that do not contain the category
            foreach ( $cart_item_keys as $cart_item_key ) {
                // Remove product from cart
                WC()->cart->remove_cart_item( $cart_item_key ); 
            }
        }
    }
}

add_action( 'woocommerce_checkout_before_customer_details', 'action_woocommerce_checkout_before_customer_details', 10, 0 );

CodePudding user response:

You need to loop first to detect any "Category A" product in the cart. If there is one, then loop again to find any product from other target categories to remove.

This edit should do it.

function action_woocommerce_checkout_before_customer_details() {
    $has_category         = false;
    $category_to_check    = array( 'cat-a' );
    $categories_to_remove = array( 'cat-b', 'cat-b', 'cat-c' );

    if ( ! is_null( WC()->cart ) ) {
        // First loop to find if a "cat-a" product exists in the cart.
        foreach ( WC()->cart->get_cart_contents() as $cart_item_key => $cart_item ) {
            $product_id = $cart_item['product_id'];
            if ( has_term( $category_to_check, 'product_cat', $product_id ) ) {
               $has_category = true;
            }
        }

        // "cat-a" product exists.
        if ( $has_category ) {
            // loop again to find if any product from categories-to-remove exist in the cart.
            foreach ( WC()->cart->get_cart_contents() as $cart_item_key => $cart_item ) {
                $product_id = $cart_item['product_id'];
                if ( has_term( $categories_to_remove, 'product_cat', $product_id ) ) {
                    // found a product belongs to other target categories, remove it.
                    WC()->cart->remove_cart_item( $cart_item_key );
                }
            }
        }
    }
}
  • Related