• Resolved Megunticook

    (@megunticook)


    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)
  • Stef

    (@serafinnyc)

    Have you tried Giftable for WooCommerce? That didn’t work? Works for us.

    Thread Starter Megunticook

    (@megunticook)

    Tried that, multiple free gift, and Chained Products.

    • This reply was modified 5 years, 7 months ago by Megunticook.

    I would like to have something like this for my store. Do you mind if I follow this thread?

    Stef

    (@serafinnyc)

    Interesting. That’s the one plugin we use and it works smoothly. You could troubleshoot by disabling all plugins except for that and WC and see. Then you can maybe locate the one plugin that’s stopping it from working.

    Thread Starter Megunticook

    (@megunticook)

    OK, thanks, it’s definitely possible there’s a plugin conflict somewhere. I’ll investigate further and report back.

    Stef

    (@serafinnyc)

    You could also take a screenshot of your rules in there so I can see. Maybe they’re not correct? Not sure. Just thinking out loud here.

    Plugin Support Hannah S.L.

    (@fernashes)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘adding free gift in cart’ is closed to new replies.