adding free gift in cart
-
I want to tell customers that if they buy 3 or more items of a particular variable product, they’ll get a free gift. It should be automatically added to the cart. I’ve tried 3 different plugins/extensions, couldn’t get those to work right, and am now trying to get a little php function in my functions.php file.
Seems like this should work, but the gift isn’t being added. Would anyone be willing to have a quick look at this function and tell me where I might be off track? Thanks.
/*add free pattern to cart when 3 or more skeins of Ikat Firefly yarn are purchased*/ add_filter( 'woocommerce_add_to_cart_validation', 'add_gift_for_yarn_purchase', 10, 3 ); function add_gift_for_yarn_purchase( $passed, $product_id, $quantity ) { global $woocommerce; /* enter array of SKUs that give gift */ $skuswithgift = array('IFF201','IFF202',"IFF203",'IFF204',"IFF205","IFF206",'IFF207',"IFF208","IFF209"); /* enter SKU of product gift */ $giftsku = 'PAT254'; /* enter name of coupon that gives 100% discount for specific product gift */ $coupon_code = 'freepattern!'; $product = wc_get_product( $product_id ); if ( $product->get_sku() && in_array( $product->get_sku(), $skuswithgift ) && ( $quantity > 2 ) ) { WC()->cart->add_to_cart( wc_get_product_id_by_sku($giftsku) ); wc_add_notice( __( 'Thank you--a free knitting pattern has been added to your cart.', 'woocommerce' ), 'success' ); $woocommerce->cart->add_discount( $coupon_code ); } return $passed; }
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘adding free gift in cart’ is closed to new replies.