本文介绍了基于Woocommerce中购物车项目数量的有条件累进百分比折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望根据购物车中的物品数量获得有条件的渐进式折扣.将2产品添加到购物车后,您将获得折扣.您添加的产品越多,您获得的折扣就越多.

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. More products you add and more discount you get.

例如:

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

我在互联网上搜索没有任何成功.在搜索折扣时,我只是依靠WooCommerce优惠券功能,或者我得到了一些旧的错误代码……

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…

有什么主意吗?我该怎么办?

Any idea? How can I do it?

有可能吗?

谢谢.

推荐答案

是的,可以使用技巧来实现此目的.通常,我们会在WooCommerce优惠券中使用购物车的折扣.优惠券在这里不适用.我将在此处使用负的按条件收费,成为折扣.

Yes its possible to use a trick, to achieve this. Normally for discounts on cart we use in WooCommerce coupons. Here coupons are not appropriated. I will use here a negative conditional fee, that becomes a discount.

计算:
—物品计数基于购物车中物品的数量和物品总数
—百分比为0.05(5%),并且随其他项的增加而增加(根据您的要求)
—我们使用折扣小计(以避免添加优惠券提供的多个折叠折扣)

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)
— We use the discounted subtotal (to avoid adding multiple collapsing discounts made by coupons)

代码:

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

代码进入您的活动子主题的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme. Tested and works.


参考:

  • WooCommerce - Adding shipping fee for free user plan
  • WooCommerce - Make a set of coupons adding a fixed fee to an order
  • WooCommerce class - WC_Cart - add_fee() method

这篇关于基于Woocommerce中购物车项目数量的有条件累进百分比折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:11