Php WooCommerce中基于购物车总数的累进折扣

Php WooCommerce中基于购物车总数的累进折扣,php,wordpress,woocommerce,cart,discount,Php,Wordpress,Woocommerce,Cart,Discount,我正在尝试在WooCommerce购物车中自动应用3种不同的优惠券代码 这是我的 add_action('woocommerce_-before_-cart','apply_-matched_-优惠券'); 函数应用匹配的优惠券(){ 全球商业; $coupon_code5='5%; $code10=百分之十; $优惠券代码55='15%'; 如果($woocommerce->cart->有折扣($优惠券\代码))返回; 如果($woocommerce->cart->cart\u content

我正在尝试在WooCommerce购物车中自动应用3种不同的优惠券代码

这是我的

add_action('woocommerce_-before_-cart','apply_-matched_-优惠券');
函数应用匹配的优惠券(){
全球商业;
$coupon_code5='5%;
$code10=百分之十;
$优惠券代码55='15%';
如果($woocommerce->cart->有折扣($优惠券\代码))返回;
如果($woocommerce->cart->cart\u contents\u total>=50&&$woocommerce->cart->cart\u contents\u total<100&&$woocommerce->cart->cart\u contents\u total!=100){
$woocommerce->cart->添加折扣($优惠券代码5);
}elseif($WOOMerce->cart->cart\u contents\u total>=100&&$WOOMerce->cart->cart\u contents\u total<150&&$WOOMerce->cart->cart\u contents\u total!=150){
$woocommerce->cart->添加折扣($优惠券编码10);
}否则{
$woocommerce->cart->add_折扣($优惠券代码15);
}
}
添加5%折扣时,此代码似乎有效,但一旦超过100欧元,则不会应用10%折扣

它只是一直在打5%的折扣


更新:

这个代码就像一个符咒。功劳归于

add_action('woocommerce_cart_计算_费用','progressive_折扣_基于_cart_总计',10,1);
基于购物车总额($cart\u对象)的渐进式折扣功能{
if(定义了('DOING'uajax'))
返回;
$cart\u total=$cart\u object->cart\u contents\u total;//购物车总计
如果($cart_total>150.00)
$percent=15;//15%
其他($cart\u总计>=100.00&$cart\u总计<150.00)
$percent=10;//10%
其他($cart\u总计>=50.00&$cart\u总计<100.00)
$percent=5;//5%
其他的
$percent=0;
如果($percent!=0){
$折扣=$cart_总计*$percent/100;
$cart\u object->add\u fee(“折扣($percent%)”,-$折扣,true);
}
}
使用具有不同购物车百分比折扣的多张优惠券是一场噩梦,因为当客户添加新商品、删除商品、更改数量以及添加(或删除)优惠券时,您必须处理这些问题

您最好使用下面这个简单的代码,它将根据购物车总金额添加购物车折扣(这里我们使用负费用,这是折扣):

add_action('woocommerce_cart_计算_费用','progressive_折扣_基于_cart_总计',10,1);
基于购物车总额($cart\u对象)的渐进式折扣功能{
if(定义了('DOING'uajax'))
返回;
$cart\u total=$cart\u object->cart\u contents\u total;//购物车总计
如果($cart_total>150.00)
$percent=15;//15%
其他($cart\u总计>=100.00&$cart\u总计<150.00)
$percent=10;//10%
其他($cart\u总计>=50.00&$cart\u总计<100.00)
$percent=5;//5%
其他的
$percent=0;
如果($percent!=0){
$折扣=$cart_总计*$percent/100;
$cart\u object->add\u fee(“折扣($percent%)”,-$折扣,true);
}
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中


这段代码经过测试并运行正常。

正如您所说,我遇到过这种情况。我在更新我的代码,一切都正常。比如,如果有人从购物车中取出一件物品,折扣的应用是正确的。但从我离开购物车,回到商店添加其他商品的那一刻起,它不会更新百分比折扣。我会试试这个,然后再回来给你。首先,感谢您抽出时间,这可能会对我有很大帮助。经过测试,您的代码运行良好。向上投票!现在让我想想你做了什么。仍然在学习PHP等,所以剖析代码并学习理解它是一个很好的实践。1.您使用的$percent有一个数值,但它是从哪里指定的?它是如何将此%折扣应用于总值的?这是WordPress在$cart\u对象中识别的某种变量吗?2.在你写的最后一个短语-$discount中,为什么在美元符号前面有一个“-”?我一直在查看以了解“添加费用”@ArneDeBelser 1)折扣适用于WC_购物车
Cart_contents\u total
属性(购物车总值),但不使用其他购物车总值,因为它们是根据此折扣计算的,并且在此挂钩中不可用。WC_Cart对象的可用方法和属性是…2)我使用负费用=>因此通常使用折扣…添加费用来添加附加费。负附加费是一种折扣。
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );

function apply_matched_coupons() {
    global $woocommerce;

$coupon_code5 = '5percent';
$coupon_code10 = '10percent';
$coupon_code55 = '15percent';

if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    if ( $woocommerce->cart->cart_contents_total >= 50 && $woocommerce->cart->cart_contents_total < 100 && $woocommerce->cart->cart_contents_total != 100 ) {

        $woocommerce->cart->add_discount( $coupon_code5 );

    } elseif ($woocommerce->cart->cart_contents_total >= 100 && $woocommerce->cart->cart_contents_total < 150 && $woocommerce->cart->cart_contents_total != 150 ) {

        $woocommerce->cart->add_discount( $coupon_code10 );

    } else {

        $woocommerce->cart->add_discount( $coupon_code15 );
    }

}
add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
function progressive_discount_based_on_cart_total( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_total = $cart_object->cart_contents_total; // Cart total

    if ( $cart_total > 150.00 )
        $percent = 15; // 15%
    elseif ( $cart_total >= 100.00 && $cart_total < 150.00 )
        $percent = 10; // 10%
    elseif ( $cart_total >= 50.00 && $cart_total < 100.00 )
        $percent =  5; // 5%
    else
        $percent = 0;

    if ( $percent != 0 ) {
        $discount =  $cart_total * $percent / 100;
        $cart_object->add_fee( "Discount ($percent%)", -$discount, true );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
function progressive_discount_based_on_cart_total( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_total = $cart_object->cart_contents_total; // Cart total

    if ( $cart_total > 150.00 )
        $percent = 15; // 15%
    elseif ( $cart_total >= 100.00 && $cart_total < 150.00 )
        $percent = 10; // 10%
    elseif ( $cart_total >= 50.00 && $cart_total < 100.00 )
        $percent =  5; // 5%
    else
        $percent = 0;

    if ( $percent != 0 ) {
        $discount =  $cart_total * $percent / 100;
        $cart_object->add_fee( "Discount ($percent%)", -$discount, true );
    }
}