根据 Woocommerce 购物车小计自动为特定产品应用优惠券
Auto apply a coupon for a specific product based on Woocommerce cart subtotal
参考这个问题:Apply a coupon programmatically in Woocommerce and then this one:How to apply an automatic discount in WooCommerce based on cart total?
这里完全是菜鸟。当购物车中其他产品的小计为 40 美元时,我需要将已有的优惠券自动应用到一个特定产品。而且我需要确保应用折扣不会因为总额低于 40 美元阈值而取出优惠券,从而造成循环。
我不确定如何修改这段代码来做到这一点:
add_action('woocommerce_before_checkout_process','add_discount_at_checkout');
function add_discount_at_checkout(){
global $woocommerce;
$minorder = 99;
if( $woocommerce->cart->get_cart()->cart_contents_total>$minorder){
//APPLY COUPON HERE
}
}
首先需要将目标优惠券代码设置为仅限于目标产品:
如果购物车小计(不包括该特定购物车商品)达到 </code>:</p>,则以下代码将仅对特定购物车商品应用特定优惠券(折扣)
<pre><code>add_action('woocommerce_before_calculate_totals', 'discount_based_on_total_threshold');
function discount_based_on_total_threshold( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Your settings
$coupon_code = 'summer'; // Coupon code
$amount_threshold = 40; // Total amount threshold
$targeted_product = 37; // Targeted product ID
// Initializing variables
$total_amount = 0;
$applied_coupons = $cart->get_applied_coupons();
$coupon_code = sanitize_text_field( $coupon_code );
$found = false;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( ! in_array( $targeted_product, array( $cart_item['product_id'], $cart_item['data']->get_id() ) ) ) {
// Get the cart total amount (excluding the targeted product)
$total_amount += $cart_item['line_total'] + $cart_item['line_tax'];
} else {
// Targeted product is found
$found = true;
}
}
// Applying coupon
if( ! in_array($coupon_code, $applied_coupons) && $found && $total_amount >= $amount_threshold ){
$cart->add_discount( $coupon_code );
}
// Removing coupon
elseif( in_array($coupon_code, $applied_coupons) && ( $total_amount < $amount_threshold || ! $found ) ){
$cart->remove_coupon( $coupon_code );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
参考这个问题:Apply a coupon programmatically in Woocommerce and then this one:How to apply an automatic discount in WooCommerce based on cart total?
这里完全是菜鸟。当购物车中其他产品的小计为 40 美元时,我需要将已有的优惠券自动应用到一个特定产品。而且我需要确保应用折扣不会因为总额低于 40 美元阈值而取出优惠券,从而造成循环。
我不确定如何修改这段代码来做到这一点:
add_action('woocommerce_before_checkout_process','add_discount_at_checkout');
function add_discount_at_checkout(){
global $woocommerce;
$minorder = 99;
if( $woocommerce->cart->get_cart()->cart_contents_total>$minorder){
//APPLY COUPON HERE
}
}
首先需要将目标优惠券代码设置为仅限于目标产品:
如果购物车小计(不包括该特定购物车商品)达到 </code>:</p>,则以下代码将仅对特定购物车商品应用特定优惠券(折扣)
<pre><code>add_action('woocommerce_before_calculate_totals', 'discount_based_on_total_threshold');
function discount_based_on_total_threshold( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Your settings
$coupon_code = 'summer'; // Coupon code
$amount_threshold = 40; // Total amount threshold
$targeted_product = 37; // Targeted product ID
// Initializing variables
$total_amount = 0;
$applied_coupons = $cart->get_applied_coupons();
$coupon_code = sanitize_text_field( $coupon_code );
$found = false;
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
if( ! in_array( $targeted_product, array( $cart_item['product_id'], $cart_item['data']->get_id() ) ) ) {
// Get the cart total amount (excluding the targeted product)
$total_amount += $cart_item['line_total'] + $cart_item['line_tax'];
} else {
// Targeted product is found
$found = true;
}
}
// Applying coupon
if( ! in_array($coupon_code, $applied_coupons) && $found && $total_amount >= $amount_threshold ){
$cart->add_discount( $coupon_code );
}
// Removing coupon
elseif( in_array($coupon_code, $applied_coupons) && ( $total_amount < $amount_threshold || ! $found ) ){
$cart->remove_coupon( $coupon_code );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。