2
votes

I am using Auto add a product for cart item from specific product categories in WooCommerce answer code to auto add a free product to the cart. The code works great if the product is in a specific category but I need to add the product if it is NOT in a specific category.

I am able to add the free product if it is not in the specific category with this edit:

if( **!** has_term( $required_categories, 'product_cat', $item['product_id'] ) ) {
    $matched_category = true;
}

But this does not remove the free product when the parent product is removed.

Any help would be appreciated!

2
What do you mean by “parent product”? Where is there a parent-child relation here? Do you actually mean, you want this free product removed again, as soon as any single product out of that specific category gets added to the cart?CBroe
I want the free product removed when the only product in the cart is from the excluded category. So in the example code from the linked question, instead of $required_categories, I would use $excluded_categories.Junky
I guess it might make more sense if you left the line shown above as it was, and instead negated the conditions at the end, if ( isset($saved_item_key) && $matched_category ) (! before $matched_category removed) and elseif ( ! isset($saved_item_key) && ! $matched_category ) (! before $matched_category added.) Can you try and see what result you get with that?CBroe

2 Answers

2
votes

Updated: Here are the changes that are required to "auto add a product in cart except for specific defined product categories (not removing the auto added product if mixed categories are in cart):

add_action( 'woocommerce_before_calculate_totals', 'auto_add_item_except_for_product_category', 10, 1 );
function auto_add_item_except_for_product_category( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Settings
    $except_terms  = array('t-shirts'); // Required product category(ies)
    $auto_added_id = 70; // Specific product to be added automatically

    $except_found  = false;
    $others_found  = false;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check for product category
        if( has_term( $except_terms, 'product_cat', $cart_item['product_id'] ) ) {
            $except_found = true;
        } else {
            $others_found = true;
        } 

        // Check if specific product is already auto added
        if( $cart_item['data']->get_id() == $auto_added_id ) {
            $auto_added_item_key = $cart_item_key; // keep cart item key
        }
    }

    // If auto added product is in cart with at least an item from a the defined product category only
    if ( isset($auto_added_item_key) && $except_found && ! $others_found ) {
        $cart->remove_cart_item( $auto_added_item_key ); // Remove specific product
    }
    // If there is at least an item from others product categories and the specific product is not in cart
    elseif ( ! isset($auto_added_item_key) && ! $except_found ) {
        $cart->add_to_cart( $auto_added_id ); // Add specific product
    }
}

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

Based on: Auto add a product for cart item from specific product categories in WooCommerce

1
votes

Might be able to hook into the Woo's remove item from cart hook:

function remove_free_item() {
    if ( is_admin() ) {
        return;
    }
    
    $product_id = 'ID_OF_FREE_ITEM';
    $product_cart_id = WC()->cart->generate_cart_id( $product_id );
    $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );

    if ( $cart_item_key ) {
        WC()->cart->remove_cart_item( $cart_item_key );
    }
}


add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart', 10, 2 );
function after_remove_product_from_cart($removed_cart_item_key, $cart) {
    // removed item
    $line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];

    // removed item product id
    $product_id = $line_item[ 'product_id' ];

    // might need to wrap this in some check depending on your case
    remove_free_item();
}