douqihou7537 2018-12-17 22:06
浏览 45
已采纳

根据属于特定运输类别的项目购物车总额更改统一费率运费

I want to implement shipping rates according to line item totals for products belonging to a specific shipping class. The rates should work for all enabled countries.

Shipping class: "flat_rate:1"

Shipping rates:

+------------------+---------------+
|  Product Total   | Shipping Rate |
+------------------+---------------+
| $50 to $150      | $190          |
| $151 to $300     | $190          |
| $301 to $700     | $255          |
| $701 to $1,000   | $305          |
| $1,001 to $2,000 | $400          |
| $2,001 to $4,000 | $430          |
| $4,001 to $6,000 | $530          |
+------------------+---------------+

So far I have seen shipping rate codes based on either cart total/quantity, country, shipping class but not shipping class+included product totals. Only paid plugins have this feature.

  • 写回答

1条回答 默认 最新

  • dth8312 2018-12-17 22:06
    关注

    Here's the solution I have made, I know it can be optimized and I welcome recommendations. Thanks to @LoicTheAztec as I referenced a lot of his answers across SO to create this solution. Tested & works on WC 3.5.2 & WP 5.0.1

    /* Changes "flat rate" shipping method rates if shipping class "industrial curtains" is present in the cart */
    function change_shipping_method_rate_based_on_shipping_class( $rates, $package )
    {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        /* HERE define your shipping class to find */
        $class = array(208);
    
        /* HERE define the shipping method to change rates for */
        $method_id1 = 'flat_rate:1';
    
        /* Checking in cart items */
        $found = false;
        $item_price = $item_qty = 0;
        foreach( WC()->cart->get_cart() as $cart_item ){
            $item_shipping_class_id = $cart_item['data']->get_shipping_class_id();
    
            if( in_array( $item_shipping_class_id, $class ) ){
                $found = true;  /* Target shipping class found */
                $item_price += $cart_item['data']->get_price(); /* Sum line item prices that have target shipping class */
                $item_qty += $cart_item['quantity']; /* Sum line item prices that have target shipping class */
                $item_total = $item_price * $item_qty; /* Get total for all products with same shipping class (There might be a better way to get this total) */
            } 
        }
    
        if( $found ) {
            if( $item_total > 0 && $item_total < 301 ) {
                $rates[$method_id1]->cost = 190;
            }
            elseif ( $item_total > 300 && $item_total < 701) {
                $rates[$method_id1]->cost = 255;
            }
            elseif ( $item_total > 700 && $item_total < 1001) {
                $rates[$method_id1]->cost = 305;
            }
            elseif ( $item_total > 1000 && $item_total < 2001) {
                $rates[$method_id1]->cost = 400;
            }
            elseif ( $item_total > 2000 && $item_total < 4001) {
                $rates[$method_id1]->cost = 430;
            }
            elseif ( $item_total > 4000 ) {
                $rates[$method_id1]->cost = 530;
            }
        }
        return $rates;
    }
    add_filter( 'woocommerce_package_rates', 'change_shipping_method_rate_based_on_shipping_class', 10, 2 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。