【问题标题】:Woocommerce - Add delivery charge to cart total for products in certain categoryWoocommerce - 将特定类别产品的运费添加到购物车总额中
【发布时间】:2017-09-07 13:44:54
【问题描述】:

如果购物车的总内容量低于 5,我将使用下面的代码添加运费。

但是,我只希望这适用于不在“配件”类别中的产品。因此,例如,如果用户有 4 个产品,但有 1 个来自“附件”类别的产品,那么它不应该包括添加来自“附件”猫的产品。

我知道我需要使用操作员来测试是否有任何产品属于“附件”类别,如果是则打破陈述,但我不确定我将如何在 Woocommerce 中执行此操作。

谁能帮帮我?

/**
 * Add custom fee on article specifics
 * @param WC_Cart $cart
 */
function add_custom_fees( WC_Cart $cart ){
    $fees = 0;


        // Check if odds and if it's the right item
        if( $cart->cart_contents_count < 5){
            get_post_meta
            $fees += 48;
        }


    if( $fees != 0 ){
        $cart->add_fee( 'Delivery charge', $fees);
    }

}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    设法弄清楚是否有人在未来陷入困境

    // Hook before adding fees 
    add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
        function add_custom_fees( WC_Cart $cart ) {
    
        $fees = 0;
        $total_products = WC()->cart->cart_contents_count;
        $total = 0;
    
        foreach( WC()->cart->get_cart() as $item ){ 
         $product = $item['data'];
    
            if( ! has_term( array( 19 ), 'product_cat', $product->id ) ){
                $total += $item['quantity'];
            } 
    
         }
    
          if($total < 5) { 
              $fees += 48;
          } 
    
    
        if( $fees != 0 ){
            // You can customize the descriptions here
            $cart->add_fee( 'Delivery charge', $fees);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2020-04-28
      • 2017-05-30
      相关资源
      最近更新 更多