Add item to woocommerce cart if other items are in a specific category

I need to be able to conditionally add only one product to a WooCommerce order / cart if any item in the cart belongs to a specific category. This is the code I tried, but it blocks the checkout and cart pages, returning NO woocommerce information or anything after (no footer, no extra page elements, etc.) Total as long as the WooCommerce data is in the front -end, with a large space after). Please if anyone can point me in the right direction it would be so much appreciated!

This is what I have in my functions.php file:

        /**
 * Checks the cart to verify whether or not a product from a Category is in the cart
 * @param $category Accepts the Product Category Name, ID, Slug or array of them
 * @return bool
 */
function qualifies_basedon_product_category( $category ) {
    foreach( WC()->cart->cart_contents as $key => $product_in_cart ) {
        if( has_term( $category, 'fee', get_id_from_product( $product_in_cart, false ) ) ) {
            return true;
        }
    }
    // Return false in case anything fails
    return false;
}

/**
 * Adds a specific product to the cart
 * @param $product_id Product to be added to the cart
 */
function add_incentive_to_cart( $product_id ) {
    // Check the cart for this product
    $cart_id = WC()->cart->generate_cart_id( $product_id );
    $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
    // Add the product only if it not in the cart already
    if( ! $prod_in_cart ) {
        WC()->cart->add_to_cart( $product_id );
    }
}



add_action( 'woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() {
    // Incentive product
    $incentive_product_id = 506;

    if( qualifies_basedon_product_category( 'fee' ) ) {
        add_incentive_to_cart( $incentive_product_id );
    } 
}

      

+3


source to share


2 answers


you can explore the following code -

add_action('woocommerce_add_to_cart', 'my_woocommerce_add_to_cart', 10, 6);

function my_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
    $incentive_product_id = 506;
    $category = 'fee';
    $taxonomy = 'fee';

    if( has_term( $category, $taxonomy, $product_id ) ){    
        $cart_id = WC()->cart->generate_cart_id( $incentive_product_id );
        $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );

        // Add the product only if it not in the cart already
        if( ! $prod_in_cart ) {
            WC()->cart->add_to_cart( $incentive_product_id );
        }
    }
}

      



Hope this helps you.

+1


source


Put this in your functions. php:

function df_add_ticket_surcharge( $cart_object ) {
global $woocommerce;
$specialfeecat = 20763; // category id you want to check for
$spfee = 0.00; // initialize special fee
$spfeeperprod = 0.00; //special fee per product
$found = $false; //required to make the code later only add the item once, also stop loops.
$product_id = 38607; //product id of the one you want to add

foreach ( $cart_object->cart_contents as $key => $value ) {

    $proid = $value['product_id']; //get the product id from cart
    $quantiy = $value['quantity']; //get quantity from cart
    $itmprice = $value['data']->price; //get product price

//check if the desired product is already in cart.
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( $_product->id == $product_id )
                $found = true;
        }
    }

//check for the category and add desired product
    $terms = get_the_terms( $proid, 'product_cat' ); //get taxonomy of the products
    if ( $terms && ! is_wp_error( $terms ) ) :
        foreach ( $terms as $term ) {
            $catid = $term->term_id;
            if($specialfeecat == $catid && ! $found) {
                WC()->cart->add_to_cart( 38607 );
            }
        }
    endif;  
   }
}
add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' );

      

I changed the code from the existing code, so spfee and spfeeperprod are still there, but you really don't need them, so I set them to 0. What I noticed what was missing to get this code to work was this part:



if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
        foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( $_product->id == $product_id )
                $found = true;
        }
    }

      

This checks the product id for the item you want to add, which is listed as "$ product_id". Hope this helps (I struggled with this for a very long time, but finally there was a breakthrough this morning!)

0


source







All Articles