【问题标题】:Change flat rate shipping rate based on item cart totals belonging to specific shipping class根据属于特定运输类别的商品购物车总数更改统一运费
【发布时间】:2018-12-17 22:06:28
【问题描述】:

我想根据属于特定运输类别的产品的订单项总计实施运费。该费率应适用于所有启用的国家/地区。

运输类别: "flat_rate:1"

运费:

+------------------+---------------+
|  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          |
+------------------+---------------+

到目前为止,我看到的运费代码基于购物车总数/数量、国家/地区、运输类别,但不是运输类别+包含的产品总数。只有付费插件才有此功能。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    这是我制定的解决方案,我知道它可以优化,欢迎提出建议。感谢@LoicTheAztec,因为我在 SO 中引用了他的很多答案来创建这个解决方案。 在 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 );
    

    【讨论】:

    • 非常好的代码!如果使用两种不同的运输等级,我将如何做同样的事情?也就是说,“208”类并更改“flat_rate:1”的价格,如您的示例和“209”类并更改“flat_rate:2”的价格@LoicTheAztec
    • 如果我正确理解了您的评论,并且在我的脑海中,将新的运输类添加到数组中:$class = array(208, 209); 删除 $method_id1 = 'flat_rate:1'; 行,然后在 if( in_array( $item_shipping_class_id, $class ) ){ 行之后添加以下行if( $item_shipping_class_id == $class[0] ){$method_id1 = 'flat_rate:1';} else {$method_id1 = 'flat_rate:2';}
    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2020-03-25
    相关资源
    最近更新 更多