根据WooCommerce中的总额自动应用百分比或固定购物车折扣 [英] Auto apply a percentage or fixed cart discount based on the total in WooCommerce
问题描述
我正在尝试在客户的WooCommerce网站上设置优惠券,以便在购物车的总金额低于上限金额或固定金额等于或大于上限金额时应用百分比折扣。
I'm trying to set up a coupon on my client's WooCommerce site so that a percentage discount applies if the total cart is below a cap amount or a fixed amount is equal or greater than the cap amount.
假设购物车的总额上限为200。如果购物车的总额低于此上限,则会应用10%的折扣。但是,如果购物车总数等于或大于200,则将固定金额20用作折扣。
Let's say that the cap for the cart total is 200. If the cart total is below this cap, 10% discount is applied. But if the cart total is 200 or greater, then the fixed amount of 20 is applied as the discount.
例如:
- 我的购物车总额为190。由于这低于上限200,因此折扣金额计算为10%,即19%。
- 我的购物车总数为210。由于此上限大于200的上限,因此将应用固定金额20。
如何设置我的WooCommerce,以根据总额应用百分比折扣或固定购物车?
How do I set my WooCommerce to apply a percentage discount or fixed cart depending on the total?
推荐答案
您可以使用自定义函数挂钩在 woocommerce_before_calculate_totals
动作挂钩中,您将在其中定义2个优惠券代码:
You can use a custom function hooked in woocommerce_before_calculate_totals
action hook where you will define 2 coupons codes:
- 百分比折扣优惠券代码(为10%)
- 固定金额的折扣优惠券代码(为$ 20)
代码:
add_action( 'woocommerce_before_calculate_totals', 'auto_add_coupons_total_based', 10, 1 );
function auto_add_coupons_total_based( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE define your coupon code
$coupon_percent = 'uget10percent'; # <=== <=== <=== <=== <=== <===
$coupon_fixed = 'uget20off'; # <=== <=== <=== <=== <=== <=== <===
// Get cart subtotal
$subtotal = 0;
foreach($cart->get_cart() as $cart_item ){
$subtotal += $cart_item['line_subtotal'];
$subtotal += $cart_item['line_subtotal_tax']; // with taxes
}
// Coupon type "percent" (less than 200)
if( $subtotal < 200 && ! $cart->has_discount( $coupon_percent ) ){
// If coupon "fixed amount" type is in cart we remove it
if( $cart->has_discount( $coupon_fixed ) )
$cart->remove_coupon( $coupon_fixed );
// Apply the "percent" type coupon code
$cart->add_discount( $coupon_percent );
}
// Coupon type "fixed amount" (Up to 200)
elseif( $subtotal >= 200 && ! $cart->has_discount( $coupon_fixed ) ) {
// If coupon "percent" type is in cart we remove it
if( $cart->has_discount( $coupon_percent ) )
$cart->remove_coupon( $coupon_percent );
// Apply the "fixed amount" type coupon code
$cart->add_discount( $coupon_fixed );
}
}
代码进入活动子主题(或活动子主题)的function.php文件主题。)
Code goes in function.php file of the active child theme (or active theme).
经过测试并可以正常工作。
Tested and works.
如果您希望将其应用于小计的不含税,您将必须对此行发表评论:
If you want to apply it on subtotal without taxes you will have to comment this line:
$subtotal += $cart_item['line_subtotal_tax']; // with taxes
或者您也可以使用负费用(因此有折扣),而不是这种方式的优惠券:
OR you can also use a negative fee (so a discount) instead of coupons this way:
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total', 25, 1 );
function discount_based_on_total( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$total = $cart->cart_contents_total;
// Percentage discount (10%)
if( $total < 200 )
$discount = $total * 0.1;
// Fixed amount discount ($20)
else
$discount = 20;
// Add the discount
$cart->add_fee( __('discount', 'woocommerce'), -$discount );
}
代码位于活动子主题(或活动主题)的function.php文件中。
Code goes in function.php file of the active child theme (or active theme).
经测试可正常工作。
这篇关于根据WooCommerce中的总额自动应用百分比或固定购物车折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!