首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于项目数量的木商运输成本研究

基于项目数量的木商运输成本研究
EN

Stack Overflow用户
提问于 2020-03-19 04:45:14
回答 1查看 989关注 0票数 2

几个小时前我发布了这个代码。我设法解决了其中一个问题,但我现在只有一个问题。此代码运行良好,但我需要使用特定的运输类将每个项目的成本乘以,然后将其添加到常规运输成本中。

例如,如果购物车中有5种产品:

  • 2使用船运-1级(产品额外运费70美元)
  • 3使用船运-2级(产品额外运费50美元)

因此,如果(例如)常规运输成本为120美元,那么运输应该是($120 + $290) =410美元

代码语言:javascript
复制
add_filter( 'woocommerce_package_rates', 'ph_add_extra_cost_based_on_shipping_class', 10, 2);
if( ! function_exists('ph_add_extra_cost_based_on_shipping_class') ) {
    function ph_add_extra_cost_based_on_shipping_class( $shipping_rates, $package ){

        $handling_fee_based_on_shipping_class = array(
            array(
                'shipping_classes'  =>  array( 'shipping-class-1'),         // Shipping Class slug array
                'adjustment'        => 70,                                  // Adjustment
            ),
            array(
                'shipping_classes'  =>  array( 'shipping-class-2' ),
                'adjustment'        =>  50,
            ),
        );

        $shipping_method_ids = array( 'cologistics-shipping' );     // Shipping methods on which adjustment has to be applied

        $adjustment = null;
        foreach( $package['contents'] as  $line_item ) {
            $line_item_shipping_class = $line_item['data']->get_shipping_class();
            if( ! empty($line_item_shipping_class) ) {
                foreach( $handling_fee_based_on_shipping_class as $adjustment_data ) {
                    if( in_array( $line_item_shipping_class, $adjustment_data['shipping_classes']) ) {
                        $adjustment = ( $adjustment_data['adjustment'] > $adjustment ) ? $adjustment_data['adjustment'] : $adjustment;
                    }
                }
            }
        }

        if( ! empty($adjustment) ) {
            foreach( $shipping_rates as $shipping_rate ) {
                $shipping_method_id = $shipping_rate->get_method_id();
                if( in_array($shipping_method_id, $shipping_method_ids) ) {
                    $shipping_rate->set_cost( (float) $shipping_rate->get_cost() + $adjustment );
                }
            }
        }

        return $shipping_rates;
    }
}

我非常感谢你的帮助。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-19 05:54:07

这不需要任何代码(通常是…)您应该设置您的传送方法如下(因此,首先删除您的代码和保存)

因此,在您的运输方法中,120将保留为全局成本。

2)然后,对于每个相关的所需运输类,您将在相应的字段中添加:

  • [qty]*70 for shipping-class-1
  • [qty]*50 for shipping-class-2
  • Calculation type: Per class: Charge shipping for each shipping class individually

这将工作,并将增加一个金额的成本,切屑类,基于购物车的项目数量。

对于基于代码的自定义送货方法,应使用以下方法来处理项目数量:

代码语言:javascript
复制
add_filter( 'woocommerce_package_rates', 'ph_add_extra_cost_based_on_shipping_class', 10, 2);
if( ! function_exists('ph_add_extra_cost_based_on_shipping_class') ) {
    function ph_add_extra_cost_based_on_shipping_class( $shipping_rates, $package ){

        $handling_fee_based_on_shipping_class = array(
            array(
                'shipping_classes'  =>  array( 'shipping-class-1'),         // Shipping Class slug array
                'adjustment'        => 70,                                  // Adjustment
            ),
            array(
                'shipping_classes'  =>  array( 'shipping-class-2' ),
                'adjustment'        =>  50,
            ),
        );

        $shipping_method_ids = array( 'cologistics-shipping' );     // Shipping methods on which adjustment has to be applied

        $adjustment = 0;

        foreach( $package['contents'] as  $line_item ) {
            $line_item_shipping_class = $line_item['data']->get_shipping_class();
            if( ! empty($line_item_shipping_class) ) {
                foreach( $handling_fee_based_on_shipping_class as $adjustment_data ) {
                    if( in_array( $line_item_shipping_class, $adjustment_data['shipping_classes']) ) {
                        $adjustment += $adjustment_data['adjustment'] * $line_item['quantity'];
                    }
                }
            }
        }

        if( $adjustment > 0 ) {
            foreach( $shipping_rates as $shipping_rate ) {
                $shipping_method_id = $shipping_rate->get_method_id();
                if( in_array($shipping_method_id, $shipping_method_ids) ) {
                    $shipping_rate->set_cost( (float) $shipping_rate->get_cost() + $adjustment );
                }
            }
        }

        return $shipping_rates;
    }
}

它应该处理汽车项目数量,增加成本…。

不要忘记刷新传送方法到传送设置,然后禁用/保存和重新启用/保存相关传送区域中的任何传送方法。

其他answer threadswoocommerce_package_rates挂钩有关。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60751052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档