php – 基于WooCommerce中购物车总额的累进折扣
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 基于WooCommerce中购物车总额的累进折扣,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2612字,纯文字阅读大概需要4分钟。
内容图文
![php – 基于WooCommerce中购物车总额的累进折扣](/upload/InfoBanner/zyjiaocheng/705/54f3dca084ea4bf48d41004c4bd13e98.jpg)
我正在尝试在WooCommerce Cart中自动应用3种不同的优惠券代码.
这是我的code!
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 );
}
}
添加5%折扣时,此代码似乎有效,但是一旦我超过100欧元,它就不会应用10%的折扣.
它只是继续应用5%的折扣.
更新:
这段代码就像一个魅力.信用到LouicTheAztek
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 );
}
}
解决方法:
Using multiple coupons with different cart percentage discount is a nightmare as you have to handle when customer add new items, remove items, change quantities and add (or remove) coupons…
您最好使用下面这个简单的代码,根据购物车总金额添加购物车折扣(这里我们使用负折扣,这是一个折扣):
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 );
}
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中.
此代码经过测试和运行.
内容总结
以上是互联网集市为您收集整理的php – 基于WooCommerce中购物车总额的累进折扣全部内容,希望文章能够帮你解决php – 基于WooCommerce中购物车总额的累进折扣所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。