2016-08-30 79 views
6

我經營的是一家WooCommerce商店,我們希望爲每位顧客提供一個免費贈品(電子書)一個產品到籃子明顯。WooCommerce - 自動添加或自動刪除購物車中的免費贈品產品

示例:
您將「product1」添加到購物籃中,購物籃現在顯示2件商品。 「產品1」和「免費贈品」。 當您從籃子中取出產品時,免費贈品將再次被移除。

我得到這個代碼現在:

add_action('woocommerce_add_to_cart', 'check_freebie_exists', 10, 6); 
function check_freebie_exists($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) { 
    if($product_id == 1234) { // obviously replace this with the product that triggers the freebie 
     /* or you could use 
     if(has_term($cat_id, 'product_cat', $product_id)) { to check if a product is in a particular product_cat 
     or you could check anything else related to the product 
     */ 
     $hasfreebie = false; 
     // loop through the cart to check the freebie is not already there 
     global $woocommerce; 
     $cart = $woocommerce->cart->get_cart(); 
     foreach($cart as $key => $values) { 
      if($values['data']->id == $your_freebie_product_id) { 
       $hasfreebie = true; 
       break; 
      } 
     } 
     if(!$hasfreebie) { 
      $woocommerce->cart->add_to_cart($your_freebie_product_id); 
     } 
    } 
} 

add_action('woocommerce_cart_item_removed', 'remove_freebie', 10, 2); 
function remove_freebie($cart_item_key, $cart) { 
    $hasmaster = false; 
    $freebiekey = NULL; 
    foreach($cart as $key => $values) { 
     if($values['data']->id == 1234) { // check that we have the product that should trigger the freebie 
      $hasmaster = true; 
     } elseif($values['data']->id == $your_freebie_product_id) { 
      $freebiekey = $key; 
     } 
    } 
    if(!$hasmaster && $freebiekey) { 
     $cart->remove_cart_item($freebiekey); 
    } 
} 

但它似乎並沒有被工作相當尚未。

我做錯了什麼?

任何幫助將非常感激。

謝謝。

+0

謝謝,我會盡快做到。 –

回答

2

- 更新 -(測試並適用於所有產品的工作)

我已經更新了你的代碼,使其工作。此代碼測試,功能齊全:

add_action('woocommerce_add_to_cart', 'check_add_freebie', 10, 6); 

function check_add_freebie($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) { 
    $item_special = $product_id; 
    $freebie_item_id = 99; // the freebie product 

    // just for our special product 
    if($product_id == $item_special) { 
     $cart_items = WC()->cart->get_cart(); 
     // Iterating through each item in cart 
     foreach ($cart_items as $cart_item){ 
      // If freebie product is in cart 
      if($cart_item['data']->id == $freebie_item_id) { 
       $has_freebie = true; 
       break; 
      } 
      // If freebie product is NOT in cart 
      else 
       $has_freebie = false; 
     } 
     // If freebie product is NOT in cart, we add it. 
     if(!$has_freebie) 
      WC()->cart->add_to_cart($freebie_item_id); 
    } 
} 

add_action('woocommerce_cart_item_removed', 'remove_freebie', 10, 2); 

function remove_freebie($cart_item_key, $product_id) { 
    $item_special = $product_id; 
    $freebie_item_id = 99; // the freebie product 
    $hasmaster = false; 
    $freebiekey = NULL; 
    // Iterating through each item in cart 
    foreach(WC()->cart->get_cart() as $item_key => $item) { 
     if ($item['data']->id == $item_special) 
      $hasmaster = true; // Special product is in cart 
     elseif ($item['data']->id == $freebie_item_id) 
      $freebiekey = $item_key; // Freebie product is in cart 
    } 
    // AS freebie product is in cart, it's removed too 
    if(!$hasmaster && !empty($freebiekey)) 
     WC()->cart->remove_cart_item($freebiekey); 
} 

的代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

該代碼已經過測試並且可以正常工作。

+0

非常感謝你Loic,這是工作完美! - 但有一個小問題。也許我沒有按自己的意願解釋自己,但我希望免費贈品能夠加入到我的產品中。因此,即使產品1,6,27,1042或621我想要添加免費贈品:-) 但是,如果它只是一個特定的產品,這個代碼工作只是完美!我只有一個類別,所以也許我可以用category_speciel更改item_speciel? –

+0

偉大的伴侶,我會等待更新,並且如果它按預期工作,則可以upvote。 –

+0

非常感謝,我無法描述我是多麼幸福,你使它工作:-)該功能已經在我的網站上生效,並且完美地工作! 接受並upvote現在是你的:-)我希望其他許多幫助可以用於類似的目的。 –

相關問題