donglingsai2880 2017-07-24 04:45
浏览 117
已采纳

根据产品类别自动添加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.

My code:

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

Thanks

  • 写回答

1条回答 默认 最新

  • dongyuan2652 2017-07-24 05:26
    关注

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

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私