有条件地自动应用优惠券以获取特定的产品ID和数量 [英] Conditionally apply coupons automatically for specific Product IDs and quantities

查看:65
本文介绍了有条件地自动应用优惠券以获取特定的产品ID和数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据产品ID和数量条件在WooCommerce商店中自动应用优惠券.我的最终目标是当两个(2)所需产品添加到购物车时自动应用一个特定的优惠券,对于另一个应用程序,每当三(3)个所需产品添加到购物车时自动应用一个特定的优惠券.单一数量的产品应无折扣.以下是该代码的正确版本,现在可以使用:

I am trying to automatically apply coupons in my WooCommerce store based on product ID and quantity conditions. My end goal is for a particular coupon to apply automatically when TWO (2) of the desired products are added to the cart, and for another coupon to app,y automatically whenever THREE (3) of the desired products are added to the cart. A single quantity of the product should have no discount. The following is the CORRECTED version of the code, which now works:

add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {

if ( !WC()->cart->is_empty() ){

    // Define HERE your Targeted Product ID and coupons codes
    $target_pid = 103;
    $coupon1 = 'soccer-sibling-2';
    $coupon2 = 'soccer-sibling-3';

    // First cart loop: Counting number of subactegory items in cart
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( $target_pid == $cart_item['data']->id ){
            // Removes any coupons in the cart already
            WC()->cart->remove_coupons();
            if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
                WC()->cart->remove_coupons();
                WC()->cart->add_discount( $coupon1 );
                wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
            } elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
               WC()->cart->remove_coupons();
                WC()->cart->add_discount( $coupon2 );
                wc_add_notice( __( 'The multiple sibling discount has been applied.', 'theme_domain' ), 'success' );
            }
            // Recalculates Cart Totals to show correct price
            WC()->cart->calculate_totals();
        }
     }
  }
}

推荐答案

您的代码中存在很多错误,也有些过时了...我已经在您的函数中重写了everith,并将其钩在了另一个钩子中.

Theres is a lot of errors in your code and it's a little obsolete too... I have rewrite everithing in your function and hooked it in another hook.

这是您重新访问的代码:

Here is your revisited code:

add_action( 'woocommerce_before_cart', 'conditional_auto_add_coupons' );
function conditional_auto_add_coupons() {

    if ( !WC()->cart->is_empty() ){

        // Define HERE your Targeted Product ID and coupons codes
        $target_pid = 103;
        $coupon1 = 'soccer-sibling-2';
        $coupon2 = 'soccer-sibling-3';

        // First cart loop: Counting number of subactegory items in cart
        foreach ( WC()->cart->get_cart() as $cart_item ){
            if( $target_pid == $cart_item['data']->id ){
                if( 2 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon1 ) ){
                    WC()->cart->add_discount( $coupon1 );
                    wc_add_notice( __( 'A quantity discount of <strong>5%</strong> has been added.', 'theme_domain' ), 'success' );
                } elseif( 3 == WC()->cart->get_cart_contents_count() && !WC()->cart->has_discount( $coupon2 ) ){
                    WC()->cart->add_discount( $coupon2 );
                    wc_add_notice( __( 'A quantity discount of <strong>10%</strong> has been added.', 'theme_domain' ), 'success' );
                }
            }
        }
    }
}

此代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

这应该可行,并且不会使您的网站崩溃,但是我还没有真正对其进行测试,因为这非常特殊.

This should work and will not make your website crash, but I haven't really test it, as this is very particular.

这篇关于有条件地自动应用优惠券以获取特定的产品ID和数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆