2017-10-17 110 views
1

我目前是客戶,誰賣線,標線塗料,他們希望他們的網站上,在工作方式如下報價:WooCommerce - 買10送3免費 - 只有最便宜的3免費

如果客戶購買10+塗料(它們可以混合和匹配),他們將獲得3個免費的,但只有最便宜的3,免費...

一個例子是像下面:

  • 的採購10 x(£99.99),y(£20.99)和z中的2(£30.99)。客戶應該收到3個最便宜的免費,所以,他們應該在這種情況下免費獲得z和y ...
  • 購買x(£99.99)中的14,y(£20.00)中的2和z 30.99)。在這種情況下,客戶應該收到免費的兩個y,並且其中一個y ...

我努力在WooCommerce中完成此任務,儘管嘗試的時間超過了我想要承認的範圍!

我希望以上是有道理的!

任何幫助或方向將不勝感激!

編輯:

我到目前爲止的代碼如下。它按順序和數量返回購物車中最便宜產品的數組。問題是,我只需要將折扣應用於3種產品,因此如果陣列中的第一個產品的數量僅爲2,我還需要將其應用到第二個最便宜的產品...等等...

function get_cheapest_x_products_in_cart($cat_id) 
{ 

global $woocommerce; 
$cat_products = []; 
$cheapest_products; 


// Add all cart items with correct category to array ($cat_products) 
foreach(WC()->cart->get_cart() as $cart_item) { 
    if(has_term($cat_id, 'product_cat', $cart_item['product_id'])) { 
     $product = wc_get_product($cart_item['product_id']); 
     $price = $product->get_regular_price(); 
     $cat_products[ 
      $cart_item['product_id'] ] = [ 
      'price' => floatval($price), 
      'quantity' => $cart_item['quantity'], 
     ]; 
    } 
} 

    uasort($cat_products, "sort_this"); 

$cheapest_three_products = array_slice($cat_products, 0, 3, true); 
return $cheapest_three_products; 

} 
+0

你試過了什麼?這種修改的哪部分是你與*掙扎? – ProEvilz

+0

@ProEvilz對不起,我應該提供更多信息。我正在努力的部分,是一種功能,可以讓最便宜的三種產品打折扣,特別是當最便宜的三種產品是一種以上產品的組合時......如果這是有道理的? – Joshua

+0

一個簡單的具有if條件的'foreach()'循環可以處理這個問題,根據您的意見判斷? – ProEvilz

回答

1

下面在woocommerce_cart_calculate_fees動作鉤子鉤住這個定製功能,客戶將獲得基於上最便宜的3個項目的價格爲每車10個項目的總和的折扣。

你必須定義一個產品類別,以及可選,你會得到一個定製的通知,當施加打折......

下面是代碼:

add_action('woocommerce_cart_calculate_fees', 'free_cheapest_3_each_10_items', 10, 1); 
function free_cheapest_3_each_10_items($wc_cart) { 
    if (is_admin() && ! defined('DOING_AJAX')) return; 

    // HERE define your product category (or categories) in the array (IDs slugs or names) 
    $cat_id = array('paints'); 
    $prices = array(); 
    $cat_id = array('clothing'); 
    $discount = $items_count = 0; 

    foreach ($wc_cart->get_cart() as $cart_item){ 
     $sale_price = $cart_item['data']->get_sale_price(); 
     // Only for the defined product category(ies) and no items in sale 
     if(has_term($cat_id, 'product_cat', $cart_item['product_id']) && (empty($sale_price) || $sale_price == 0)) { 
      for($i = 0; $i < $cart_item['quantity']; $i++){ 
       $prices[] = floatval($cart_item['data']->get_regular_price()); 
       $items_count++; 
      } 
     } 
    } 

    if($items_count >= 10){ 
     // Ordering prices 
     asort($prices); 

     // Get the occurence number for 3 free items each 10 items 
     for($i = 0, $j = -1; $i < $items_count; $i++) 
      if($i % 10 == 0) $j++; 

     $count = $j*3; 

     // Get the 3 free items for each 10 items in cart (for the defined product category(ies)) 
     $free_cheapest_items = array_slice($prices, 0, $count, true); 

     // Calculate the discount amount 
     foreach($free_cheapest_items as $item_price) 
      $discount -= $item_price; 

     // The discount 
     if($discount != 0){ 
      $wc_cart->add_fee("Bulk discount", $discount, true); 

      // Displaying a custom notice (optional) 
      wc_clear_notices(); 
      wc_add_notice(__("You get $count free items for the $items_count items in cart"), 'notice'); 
     } 
    } 
} 

代碼放在功能。你的活動兒童主題(或主題)的php文件或任何插件文件。

在WooCommerce 3上測試並工作。

+0

非常感謝你!儘管我有兩個問題 - 在'//獲得每個10個項目的3個免費項目的發生次數',那麼這個部分究竟做了什麼?我正在努力遵循這一部分...此外 - 我只希望它應用一次,所以不是每個10的倍數 - 我將如何實現這一目標? – Joshua

+0

@Joshua所以,只需將'$ count = $ j * 3;'改爲'$ count = 3;'...... – LoicTheAztec