0
votes

I have searched far and wide for a solution to add a free item (that I have hidden with woocommerce) to the cart when someone enters the hiddenproduct coupon I made. This is the code that I am using, it is a modified version of this:http://docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/. The difference is instead of using the cart total to add it, I am trying to use the applied coupon.

Here is my current code and it is not adding the product to the cart:

add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
  if ( ! is_admin() ) {
    global $woocommerce;
    $product_id = 1211;
    $found = false;
    $coupon_id = 1212;

    if( $woocommerce->cart->applied_coupons == $coupon_id ) {
        //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( $product_id );
        } else {
            // if no products in cart, add it
                $woocommerce->cart->add_to_cart( $product_id );
            }
        }
    }
}

I am pretty sure that the error is happening here '( $woocommerce->cart->applied_coupons == $coupon_id )' but I do not know the correct identifiers.

This is in my functions.php

Can anyone help me out? Thank you

2
if I use if( $woocommerce->cart->applied_coupons) { I can get it to add the product to the cart, but it does that with any coupon. How can i build upon that to only add it using the specific coupon ID?CSum

2 Answers

1
votes

I was in a similar situation and used some of your code and made some tweaks.. This worked for me: if(in_array($coupon_id, $woocommerce->cart->applied_coupons)){

Cheers!

0
votes

I know this is ancient, but I had basically the same need. So I thought I'd go ahead and post my solution. I'm hardly an expert on any of this, so I'm sure there's plenty of room for improvement. I put this in my functions.php (obviously borrowing a lot from the original post here):

function mysite_add_to_cart_shortcode($params) {

    // default parameters
    extract(shortcode_atts(array(
        'prod_id' => '',
        'sku' => '',
    ), $params));

    if( $sku && !$prod_id ) $prod_id = wc_get_product_id_by_sku($sku);

    if($prod_id) {

        $cart_contents = WC()->cart->get_cart_contents();
        if ( sizeof( $cart_contents ) > 0 ) {
            foreach ( $cart_contents as $values ) {
                $cart_prod = $values['product_id'];
                if ( $cart_prod == $prod_id ) $found = true;
            }
            // if product not found, add it
            if ( ! $found ) WC()->cart->add_to_cart( $prod_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $prod_id );
        }

    }

  return '';
}
add_shortcode('mysite_add_to_cart','mysite_add_to_cart_shortcode');


add_action( 'woocommerce_applied_coupon', 'mysite_add_product' );
function mysite_add_product($coupon_code) {

    $current_coupon = new WC_Coupon( $coupon_code );
    $coupon_description = $current_coupon->get_description();

    do_shortcode($coupon_description);

    return $coupon_code;
}

This lets me add a shortcode in the coupon description that specifies what product should get added when the coupon is applied. It can be either [mysite_add_to_cart prod_id=1234] or [mysite_add_to_cart sku=3456]. So far it seems to be working fine.