根据 WooCommerce 中的总数自动应用百分比或固定购物车折扣
Auto apply a percentage or fixed cart discount based on the total in WooCommerce
我正在尝试在客户的 WooCommerce 网站上设置优惠券,以便在总购物车低于上限金额或固定金额等于或大于上限金额时应用百分比折扣。
假设购物车总数上限为 200。如果购物车总数低于此上限,则应用 10% 的折扣。但是,如果购物车总数为 200 件或更多,则应用固定金额 20 作为折扣。
例如:
- 我的购物车总数是 190。由于这少于 200 的上限,折扣金额计算为 10%,即应用 19
- 我的购物车总数是 210。由于这大于 200 的上限,因此应用固定数量 20。
如何设置我的 WooCommerce 以根据总数应用百分比折扣或固定购物车?
您可以使用挂钩在 woocommerce_before_calculate_totals
操作挂钩中的自定义函数,您将在其中定义 2 个优惠券代码:
- 百分比折扣券代码(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 文件。
已测试并有效。
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
或者您也可以这样使用负费用(即折扣)代替优惠券:
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 ()
else
$discount = 20;
// Add the discount
$cart->add_fee( __('discount', 'woocommerce'), -$discount );
}
代码进入活动子主题(或活动主题)的 function.php 文件。
已测试并有效。
好的,所以我终于弄清楚如何使用此代码,以便它仅在添加优惠券时触发。我还添加了一个脚本来通知买家已达到优惠券折扣限制
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;
// HERE define your coupon code
$coupon_percent = 'xyz20'; # <=== <=== <=== <=== <=== <===
$coupon_fixed = 'fixedamount'; # <=== <=== <=== <=== <=== <=== <===
// 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
}
//Set HERE the limit amount
$limit = 40; //without Tax
// 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 );
}
// Coupon type "fixed amount" (Up to 200)
if( $subtotal >= 200 && $cart->has_discount( $coupon_percent ) ) {
// 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 );
// Displaying a custom message
$message = __( "The total discount limit of $$limit has been reached", "woocommerce" );
wc_add_notice( $message, 'notice' );
}
}
我正在尝试在客户的 WooCommerce 网站上设置优惠券,以便在总购物车低于上限金额或固定金额等于或大于上限金额时应用百分比折扣。
假设购物车总数上限为 200。如果购物车总数低于此上限,则应用 10% 的折扣。但是,如果购物车总数为 200 件或更多,则应用固定金额 20 作为折扣。
例如:
- 我的购物车总数是 190。由于这少于 200 的上限,折扣金额计算为 10%,即应用 19
- 我的购物车总数是 210。由于这大于 200 的上限,因此应用固定数量 20。
如何设置我的 WooCommerce 以根据总数应用百分比折扣或固定购物车?
您可以使用挂钩在 woocommerce_before_calculate_totals
操作挂钩中的自定义函数,您将在其中定义 2 个优惠券代码:
- 百分比折扣券代码(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 文件。
已测试并有效。
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
或者您也可以这样使用负费用(即折扣)代替优惠券:
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 ()
else
$discount = 20;
// Add the discount
$cart->add_fee( __('discount', 'woocommerce'), -$discount );
}
代码进入活动子主题(或活动主题)的 function.php 文件。
已测试并有效。
好的,所以我终于弄清楚如何使用此代码,以便它仅在添加优惠券时触发。我还添加了一个脚本来通知买家已达到优惠券折扣限制
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;
// HERE define your coupon code
$coupon_percent = 'xyz20'; # <=== <=== <=== <=== <=== <===
$coupon_fixed = 'fixedamount'; # <=== <=== <=== <=== <=== <=== <===
// 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
}
//Set HERE the limit amount
$limit = 40; //without Tax
// 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 );
}
// Coupon type "fixed amount" (Up to 200)
if( $subtotal >= 200 && $cart->has_discount( $coupon_percent ) ) {
// 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 );
// Displaying a custom message
$message = __( "The total discount limit of $$limit has been reached", "woocommerce" );
wc_add_notice( $message, 'notice' );
}
}