【问题标题】:Add WooCommerce coupon code automatically based on product categories根据产品类别自动添加 WooCommerce 优惠券代码
【发布时间】:2017-12-29 13:53:16
【问题描述】:

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

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

我的代码:

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

谢谢

【问题讨论】:

    标签: php wordpress woocommerce categories coupon


    【解决方案1】:

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

    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+ 上经过测试并且可以运行。

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

    【讨论】:

    • 效果很好,感谢您让代码看起来很干净。
    • @LoicTheAztec 但是当您手动创建订单时,这也可以在 wp-admin 中使用吗?
    猜你喜欢
    • 2014-11-26
    • 2018-04-12
    • 1970-01-01
    • 2021-06-10
    • 2019-07-24
    • 2022-01-09
    • 2019-02-17
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多