2016-11-08 114 views
2

我有一個woocommerce功能,可自動添加購物車免費禮物。有條件地添加或刪除購物車中的免費產品

我想基於15

最低數量添加此我這個代碼的作品,但是當我更新的車不要刪除項目,如果數量不是15

當我更新購物車時,如果總計ID不同於15,我該如何自動刪除產品?

} 
//add_action('init', 'wcsg_add_product_to_cart'); 
add_action('wp_loaded', 'wcsg_add_product_to_cart', 99); 
function wcsg_add_product_to_cart() { 
if (! is_admin()) { 
    global $woocommerce; 
      //calcoliamo quanti prodotti ci sono nel carrello 
      $totalecarrello = $woocommerce->cart->cart_contents_count; 
      echo "<script type='text/javascript'>alert('$totalecarrello');</script>"; 
    $cart = WC()->cart->get_cart(); 
    $wcsgProduct = '0'; 
    $wcsgProduct = get_option('wcsgProduct'); 
    $product_id = $wcsgProduct; 
    if ($product_id== '0' || $product_id == NULL) { 
     // do nothing 
    } else { 
     $found = false; 
     if (sizeof($cart) > 0) { 
      foreach ($cart as $cart_item_key => $values) { 
       $_product = $values['data']; 
       if ($_product->id == $product_id) 
        $found = true; 
      } 
          //controlliamo quanti prodotti ci sono 
      if (! $found && $totalecarrello == 15)  
           //se sono 15 aggiungiamo il prodotto free 
       WC()->cart->add_to_cart($product_id); 
      $message = $woocommerce->cart->cart_contents_count; 
echo "<script type='text/javascript'>alert('$message');</script>"; 
$totalecarrello = $woocommerce->cart->cart_contents_count; 
//altrimenti no 
     } else { 
      //WC()->cart->add_to_cart($product_id); 
      WC()->cart->remove_cart_item($product_id); 
     } 
    } 
} 
} 

感謝您的幫助

回答

2

是更好地使用woocommerce_before_calculate_totals專用WooCommerce鉤,因爲它處理所有的變化,客戶可以購物車頁面上做(刪除項目,更新量)。

這是代碼:

add_action('woocommerce_before_calculate_totals', 'wcsg_adding_promotional_product', 10, 1); 
function wcsg_adding_promotional_product($cart_object) { 

    if(!is_cart()) return; 

    $promo_id = get_option('wcsgProduct'); // Getting the ID of your promotional product 
    $targeted_cart_items = 15; // <=== Set HERE the targeted number of items in cart 
    $cart_count = $cart_object->cart_contents_count; // Items in cart 
    $has_promo = false; 

    if (!$cart_object->is_empty() && is_cart() && !empty($promo_id)){ 

     // Iterating through each item in cart 
     foreach ($cart_object->cart_contents as $key => $cart_item){ 

      // If Promo product is in cart 
      if($cart_item['data']->id == $promo_id) { 
       $has_promo = true; 
       $promo_key= $key; 
      } 
     } 
     //If Promo product is NOT in cart and targeted item count is reached, we add it. 
     if(!$has_promo && $cart_count >= $targeted_cart_items) 
      $cart_object->add_to_cart($promo_id); 

     // If Promo product is in cart and targeted item count NOT reached, we remove it. 
     if($has_promo && $cart_count <= $targeted_cart_items) 
      $cart_object->remove_cart_item($promo_key); 

    } 
} 

代碼下去你的活躍兒童主題(或主題)的function.php文件或任何插件文件。

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


基於:Adding a promotional product when a certain cart amount is reached

相關主題:WooCommerce - Auto add or auto remove a freebie product from cart

相關問題