首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据产品类别自动添加WooCommerce优惠券代码

根据产品类别自动添加WooCommerce优惠券代码
EN

Stack Overflow用户
提问于 2017-07-24 04:45:22
回答 1查看 2.2K关注 0票数 3

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

但我看不出总有什么变化,请帮帮我。

我的代码:

代码语言:javascript
复制
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>';
            }
        }
    }
}

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-24 05:26:54

以下是正确的钩子和代码,当购物车项目来自特定的产品类别并更新购物车总数时,它将自动应用优惠券代码:

代码语言:javascript
复制
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+上测试和工作的。

如果优惠券已被应用,并且特定产品类别的购物车项目都被移除,则优惠券也将被移除。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45272782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档