简体   繁体   English

根据Woocommerce购物车小计自动为特定产品应用优惠券

[英]Auto apply a coupon for a specific product based on Woocommerce cart subtotal

Referring to this question: Apply a coupon programmatically in Woocommerce and then this one: How to apply an automatic discount in WooCommerce based on cart total? 提到这个问题: 在Woocommerce中以编程方式应用优惠券 ,然后这个问题: 如何基于购物车总额在WooCommerce中应用自动折扣?

Complete rookie here. 在这里完成菜鸟。 I need to auto-apply an already existing coupon to one particular product when the subtotal of other products in the cart is $40. 当购物车中其他产品的小计为$ 40时,我需要将现有的优惠券自动应用于一种特定产品。 And I need to make sure that applying the discount doesn't create a loop by taking the coupon out due to dropping the total below the $40 threshold. 而且我需要确保应用折扣不会由于将总金额降至40美元以下的门槛而取出优惠券,从而不会造成循环。

I'm not sure how to modify this code to do that: 我不确定如何修改此代码来做到这一点:

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

}
}


First the targeted coupon code need to be set as restricted to the targeted product only: 首先,需要将目标优惠券代码设置为仅限于目标产品:

在此处输入图片说明

The following code will apply a specific coupon (a discount) just to a specific cart item if the cart subtotal (excluding this specific cart item) is up to $40 : 如果购物车小计(不包括此特定购物车项目)的价格最高为$40 ,则以下代码将对特定购物车项目应用特定优惠券(折扣):

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 );
    }
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 WooCommerce 购物车小计自动添加可变数量的特定产品 - Auto add a variable quantity of specific product based on WooCommerce cart subtotal 根据Woocommerce中的购物车项目自动应用/删除优惠券代码 - Auto apply / remove a coupon code based on cart items in Woocommerce 如何在 WooCommerce 中自动生成优惠券并将其应用于购物车? - How to auto generate a coupon in WooCommerce and apply it to the cart? WooCommerce 特定优惠券折扣基于产品类别 - WooCommerce specific coupon discount based on product category WooCommerce 中特定购物车小计的免费赠品产品 - Free Give-Away product for specific cart subtotal in WooCommerce 根据 WooCommerce 中的购物车小计将特定的应税运费成本设置为 0 - Set specific taxable shipping rate cost to 0 based on cart subtotal in WooCommerce 根据Woocommerce中的其他购物车项目自动将特定产品添加到购物车 - Auto add a specific product to cart based on other cart items count in Woocommerce 根据在商店woocommerce中的总支出自动申请优惠券 - Auto apply coupon based on total spent in shop woocommerce 在Woocommerce中基于自定义user_meta自动应用优惠券 - Auto apply coupon based on custom user_meta in Woocommerce 仅在 WooCommerce 中自动应用特定国家/地区的优惠券折扣 - Auto apply a coupon discount for specific countries only in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM