2017-10-17 120 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