根据产品类别自动添加WooCommerce优惠券代码 [英] Add WooCommerce coupon code automatically based on product categories

查看:63
本文介绍了根据产品类别自动添加WooCommerce优惠券代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果购物车中有特定类别的产品,我会尝试自动添加优惠券代码。显示折扣金额后,必须在总计中更新价格。

I am trying to add a coupon code automatically if the cart has products from specific categories. The price must be updated in the Total after showing the discount amount.

但是我看不到总数有任何变化,请帮助我。

But I cant see any changes in the total, kindly help me.

我的代码:

add_action('wc_cart_product_subtotal' , 'getsubtotalc');
function getsubtotalc ($product_subtotal, $_product, $quantity, $object) {
    if( is_cart() || is_checkout() ) {
        global $woocommerce, $product;
        global $total_qty;
        /*$coupon_code = 'drawer';
        if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;*/

        foreach ( $woocommerce->cart->cart_contents as $product ) {
            if( has_term( array('t-shirts-d','socks-d','joggers-d','boxers-d'), 'product_cat', $cart_item['product_id'] ) ){
                $coupon_code = 'drawer';
                if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code))) {
                    $woocommerce->show_messages();
                }
                echo '<div class="woocommerce_message"><strong>The number of Product in your order is greater than 10 so a 10% Discount has been Applied!</strong>
 </div>';
            }
        }
    }
}

推荐答案

这是正确的挂钩和代码,当购物车中的商品来自特定产品类别时,该代码将自动应用优惠券代码。将会更新购物车总计:

Here is the correct hook and code, that will auto apply a coupon code when a cart item is from specific product categories and will update cart totals:

add_action( 'woocommerce_before_calculate_totals', 'wc_auto_add_coupons_categories_based', 10, 1 );
function wc_auto_add_coupons_categories_based( $cart_object ) {

    // HERE define your product categories and your coupon code
    $categories = array('t-shirts-d','socks-d','joggers-d','boxers-d');
    $coupon = 'drawer';

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Initialising variables
    $has_category = false;

    //  Iterating through each cart item
    foreach ( $cart_object->get_cart() as $cart_item ) {
        // If a cart item belongs to a product category
        if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
            $has_category = true; // Set to true
            break; // stop the loop
        }
    }

    // If conditions are matched add the coupon discount
    if( $has_category && ! $cart_object->has_discount( $coupon )){
        // Apply the coupon code
        $cart_object->add_discount( $coupon );

        // Optionally display a message 
        wc_add_notice( __('my message goes here'), 'notice');
    } 
    // If conditions are not matched and coupon has been appied
    elseif( ! $has_category && $cart_object->has_discount( $coupon )){
        // Remove the coupon code
        $cart_object->remove_coupon( $coupon );

        // Optionally display a message 
        wc_add_notice( __('my warning message goes here'), 'alert');
    }
}

代码进入function.php文件您的活动子主题(或主题)或任何插件文件中。

代码已在woocommerce 3+上进行了测试,并且可以正常工作。

Code is tested on woocommerce 3+ and works.


如果已应用优惠券,并且所有特定产品类别的购物车商品均被删除,则优惠券也将被删除。

If the coupon has been applied and the cart items from specific product categories are all removed, the coupon is also removed.

这篇关于根据产品类别自动添加WooCommerce优惠券代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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