Arnold
Arnold

Reputation: 1

Add item to cart automatically when another product is present

I'm using dynamic pricing to discount an item to free one another is present, i want to avoid having the client add the free product as a separate step and just let the system add it.

I started off with the snippet but I can not get this to work when the item is present

this is what i got so far:

<?php

function add_product_to_cart() {
  if ( ! is_admin() ) {
        global $woocommerce;
        $product_id_gift = 2287;
        $found = false;
        $product_id = 30;
        $incart_free = false;

        foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
                if ( $_product->id == $product_id ){
                $incart_free = true;
            }
        return $incart_free;
}
        if( $incart_free == true ) {
            //check if product 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_gift )
                        $found = true;
                }
                // if product not found, add it
                if ( $found != true  )
                    $woocommerce->cart->add_to_cart( $product_id_gift );
            } else {
                // if no products in cart, add it
                $woocommerce->cart->add_to_cart( $product_id_gift );
            }
        }
    }
}

add_action( 'init', 'add_product_to_cart' );

?>

Thank you!

Upvotes: 0

Views: 4884

Answers (2)

Daniel_ZA
Daniel_ZA

Reputation: 574

If I understood your question correctly you are looking to make an item free if certain items are in the shopping cart. Here is my solution:

1. Create a coupon for WooCommerce in Wordpress. Make the coupon amount 100% and the discount type 'Product% Discount'. Go to Usage Restriction->Products and specify the specific product you want to be free, this will make the coupon only apply to that specific product.

2. Create a function that firsts checks if there are specific items present in the cart and if so, then adds and discounts the item you want to be free to the cart. The following code will do the trick (I have tested it and it worked fine, although it is not the cleanest solution):

add_action( 'init', 'product_discount' );

function product_discount(){

//variable declerations.
global $woocommerce;
$product_id = 1; // product to add
$products= array('2', '3', '4'); //specific product(s) to be present in the cart
$coupon_code = 'abc'; // coupon code from wp

//get the cart contents.
$cart_items = $woocommerce->cart->get_cart();   
//check if the cart is not empty.
if(sizeof($cart_items) > 0){    
    //loop through the cart items looking for the specific products.
    foreach ($cart_items as $key => $item){     
        //check if the cart items match to any of those in the array and check if the desired product is in the cart.
        if(in_array($item['product_id'], $products) && $item['product_id'] != $product_id){ 
                //add course.
                $woocommerce->cart->add_to_cart($product_id);
                //discount course.
                $woocommerce->cart->add_discount(sanitize_text_field($coupon_code));
            }else{
                break; //to prevent the product from being added again for the next loop.
            }
    }           
}   
}

Hope this helps!

Upvotes: 1

Rohil_PHPBeginner
Rohil_PHPBeginner

Reputation: 6080

Your logic is completely wrong here because if ( $_product->id == $product_id_gift ) will never gonna be true as both product_id is different.

So logic should be :

1. Check all the products that are added in to the cart
2. Check that if any product in the cart having free products or not
3. If yes, then simply add the free product.

So code would be something like this :

    add_action( 'init', 'add_product_to_cart' );

    function add_product_to_cart() {
    if ( ! is_admin() ) {
    global $woocommerce;
    $product_id = 30; //Your product ID here
    $free_product_id = 2287; //Free product ID here
    $found = false;
    //check if product 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;
    }
    }
    // if product not found, add it
    if ( $found )
    $woocommerce->cart->add_to_cart( $free_product_id );
    }
    }
    }

NOTE: Untested.So let me know the output as well.

Upvotes: 0

Related Questions