简体   繁体   English

基于 Woocommerce 中购物车项目数的有条件累进百分比折扣

[英]Conditional progressive percentage discount based on cart item count in Woocommerce

I would like to have a conditional progressive discount based on number of items in cart.我想根据购物车中的商品数量获得有条件的累进折扣。 After you added 2 products to the cart, you get a discount.将 2 件产品添加到购物车后,您将获得折扣。 More products you add and more discount you get.您添加的产品越多,您获得的折扣就越多。

For example:例如:

  • 1 product – full price (No Discount) 1 件产品 – 全价(无折扣)
  • 2 products – full price with 5% discount of the combined price 2 件产品 – 全价,总价的 5% 折扣
  • 3 products – full price with 10% discount of the combined price 3 件产品 – 全价,总价的 10% 折扣
  • 4 products – full price with 15% discount of the combined price 4 件产品 – 全价,总价的 15% 折扣
  • And so on …等等 …

I have search over internet without any success.我在互联网上搜索没有任何成功。 When searching about discounts I just fall on WooCommerce coupon feature or I get some old wrong code…在搜索折扣时,我只是使用了 WooCommerce 优惠券功能,或者我得到了一些旧的错误代码……

Any idea?任何的想法? How can I do it?我该怎么做?

Is it possible?是否有可能?

Thanks.谢谢。

Update - October 2018 (code improved)更新 - 2018 年 10 月(代码改进)

Yes its possible to use a trick, to achieve this.是的,可以使用技巧来实现这一点。 Normally for discounts on cart we use in WooCommerce coupons.通常用于我们在 WooCommerce 优惠券中使用的购物车折扣。 Here coupons are not appropriated.这里不使用优惠券。 I will use here a negative conditional fee, that becomes a discount .我将在这里使用负条件费用,即成为折扣

The calculation:计算:
— The item count is based on quantity by item and total of items in cart — 商品数量基于商品数量和购物车中的商品总数
— The percent is 0.05 (5%) and it grows with each additional item (as you asked) — 百分比为 0.05 (5%),并且随着每个额外项目的增加而增长(如您所问)
— We use the discounted subtotal (to avoid adding multiple collapsing discounts made by coupons) — 我们使用折扣小计(避免添加多个由优惠券造成的折叠折扣)

The code:代码:

add_action( 'woocommerce_cart_calculate_fees', 'cart_progressive_discount', 50, 1 );
function cart_progressive_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // For 1 item (quantity 1) we EXIT;
    if( $cart->get_cart_contents_count() == 1 )
        return;

    ## ------ Settings below ------- ##

    $percent = 5; // Percent rate: Progressive discount by steps of 5%
    $max_percentage = 50; // 50% (so for 10 items as 5 x 10 = 50)
    $discount_text = __( 'Quantity discount', 'woocommerce' ); // Discount Text

    ## ----- ----- ----- ----- ----- ##

    $cart_items_count = $cart->get_cart_contents_count();
    $cart_lines_total = $cart->get_subtotal() - $cart->get_discount_total();

    // Dynamic percentage calculation
    $percentage = $percent * ($cart_items_count - 1);

    // Progressive discount from 5% to 45% (Between 2 and 10 items)
    if( $percentage < $max_percentage ) {
        $discount_text .=  ' (' . $percentage . '%)';
        $discount = $cart_lines_total * $percentage / 100;
        $cart->add_fee( $discount_text, -$discount );
    }
    // Fixed discount at 50% (11 items and more)
    else {
        $discount_text .=  ' (' . $max_percentage . '%)';
        $discount = $cart_lines_total * $max_percentage / 100;
        $cart->add_fee( $discount_text, -$discount );
    }
}

Code goes in function.php file of your active child theme.代码位于活动子主题的 function.php 文件中。 Tested and works.测试和工作。

When using FEE API for discounts (a negative fee), taxes are always applied.使用 FEE API 进行折扣(负费用)时,始终应用税费。


References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM