2016-08-30 27 views
6

Tôi đang điều hành một cửa hàng WooCommerce, nơi chúng tôi muốn cung cấp cho mọi khách hàng một freebie (sách điện tử) sẽ hiển thị trong giỏ hàng sau khi bạn thêm một sản phẩm cho giỏ rõ ràng.WooCommerce - Tự động thêm hoặc tự động xóa sản phẩm freebie khỏi giỏ hàng

Ví dụ:
Bạn thêm "product1" vào giỏ và giỏ bây giờ sẽ hiển thị 2 sản phẩm. "product1" và "freebie". Khi bạn tháo sản phẩm khỏi giỏ, freebie sẽ bị xóa một lần nữa.

Tôi có mã này cho bây giờ:

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); 
    } 
} 

Nhưng nó dường như không có tác dụng khá được nêu ra.

Tôi đang làm gì sai?

Mọi trợ giúp sẽ thực sự được đánh giá cao.

Cảm ơn.

+0

Cảm ơn bạn, tôi sẽ làm điều đó càng sớm càng tốt. –

Trả lời

2

- Cập nhật -(thử nghiệm và làm việc cho tất cả các sản phẩm)

Tôi đã cập nhật mã của bạn, để làm cho nó làm việc. Mã này đã được thử nghiệm và đầy đủ chức năng:

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); 
} 

mã đi trong tập tin function.php của hoạt động child theme của bạn (hoặc chủ đề) hoặc cũng trong bất kỳ tập tin plugin.

Mã này được kiểm tra và hoạt động.

+0

Cảm ơn bạn rất nhiều Loic, điều này đang làm việc hoàn hảo! - Nhưng có một vấn đề nhỏ. Có lẽ tôi đã không giải thích bản thân mình như dự định, nhưng tôi muốn các freebie được thêm vào giỏ, với tất cả các sản phẩm của tôi. Vì vậy, ngay cả khi đó là sản phẩm 1, 6, 27, 1042 hoặc 621 tôi muốn các freebie được thêm vào :-) Nhưng nếu nó được dự định chỉ là một sản phẩm cụ thể, mã này hoạt động chỉ perfekt! Tôi chỉ có một thể loại, vì vậy có lẽ tôi có thể thay đổi item_speciel với category_speciel? –

+0

Bạn đời tuyệt vời, tôi sẽ đợi bản cập nhật và cập nhật nếu nó hoạt động như dự định. –

+0

Cảm ơn bạn rất nhiều, tôi không thể mô tả tôi hạnh phúc như thế nào, rằng bạn đã làm cho nó hoạt động :-) Tính năng này hiện đã có trên trang web của tôi và hoạt động hoàn hảo! Được chấp nhận và upvote bây giờ là của bạn :-) Tôi hy vọng nhiều trợ giúp khác có thể sử dụng điều này cho các mục đích tương tự. –

Các vấn đề liên quan