Buy one get one in woocommerce with out coupon cod

2019-07-31 02:46发布

I want implement functionality on my site when user buy a x product then user will get a y product as free.

In my site i have variable products.If customer buy 1kg pack of X product then customer want to get Free 30ml of Y product.

I added below code in my function.php but its working the problem is when refreshing the page gift product count is increasing. my updated code.

    add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );

function bbloomer_apply_matched_coupons() {

    global $woocommerce;
 $cat_in_cart = false;
    foreach ( $woocommerce->cart->cart_contents as $key => $values ) {

    // this is your product ID
    $autocoupon = array( 123411 );

    if( in_array( $values['variation_id'], $autocoupon ) ) {  
       $cat_in_cart = true;
        break;

    }


    }
 if ( $cat_in_cart ) {   
$product_id   = 2044;
$quantity     = 1;
$variation_id = 2046;
$variation    = array(
    'pa_size'  => '30ml'
);

$woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
 }

}

2条回答
爷的心禁止访问
2楼-- · 2019-07-31 02:51

There is an other alternative based on quantity using another hook… It handles when quantities are changed or products removed from cart and set the price of the free product to zero:

add_action( 'woocommerce_before_calculate_totals', 'add_free_product_to_cart', 20, 1 );
function add_free_product_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $products_ids = array( 123411 ); // Products IDs to check
    $free_var_id  = 2046; // free product (variation ID)
    $free_prod_id = 2044; // free product (product ID)
    $free_attr    = array( 'pa_size'  => 'medium' ); // Free product attribute
    $free_price   = 0;

    $targeted = $free = array('qty' => 0 ); // Initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( in_array( $cart_item['data']->get_id(), $products_ids ) ) {
            $targeted['qty'] += $cart_item['quantity'];
        } elseif ( $cart_item['data']->get_id() == $free_var_id ) {
            $cart_item['data']->set_price($free_price);
            $free['qty'] += $cart_item['quantity'];
            $free['key'] = $cart_item_key;
        }
    }
    // We exit (No changes are needed)
    if( $targeted['qty'] == $free['qty'] )
        return;

    if( $targeted['qty'] > 0 && $free['qty'] == 0 )
    {
        // Add the free product
        $cart->add_to_cart( $free_prod_id, $targeted['qty'], $free_var_id, $free_attr );
    }
    elseif(  $targeted['qty'] > 0 && $free['qty'] > 0 && $targeted['qty'] != $free['qty'] )
    {
        // Adjust free product quantity
        $cart->set_quantity( $free['key'], $targeted['qty'] );
    }
    elseif(  $targeted['qty'] == 0 && $free['qty'] > 0)
    {
        // Remove free product
        $cart->remove_cart_item( $free['key'] );
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

查看更多
smile是对你的礼貌
3楼-- · 2019-07-31 03:08

Add this to start of 'functions.php'.

ob_start();
session_start();

Then add this code.

add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );

function bbloomer_apply_matched_coupons() {

    if(!$_SESSION['GiftAdded']) {
        global $woocommerce;
        $cat_in_cart = false;
        $coupon_in_cart = false;

        $autocoupon = array( 123411 ); // variation ids of products that offers gifts
        $freecoupon = array( 2046 ); // variation ids of products that are gift coupons

        foreach ( $woocommerce->cart->cart_contents as $key => $values ) {      
            if( in_array( $values['variation_id'], $autocoupon ) ) {  
                $cat_in_cart = true;                
            }
            if( in_array( $values['variation_id'], $freecoupon) ) {  
                $coupon_in_cart = true;             
            }
        }

         if ( $cat_in_cart && !$coupon_in_cart ) {   
            $product_id   = 2044;
            $quantity     = 1;
            $variation_id = 2046;
            $variation    = array( 'pa_size'  => '30ml' );

            $woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
            $_SESSION['GiftAdded']=true;
        }
    }
}

The $_SESSION['GiftAdded'] will prevent the gift product added again when its deleted manually.

查看更多
登录 后发表回答