【问题标题】:Woocommerce shipping cost based on item quantity for specific shipping classWoocommerce 运输成本基于特定运输类别的物品数量
【发布时间】:2020-03-19 04:45:14
【问题描述】:

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

例如,如果我的购物车中有 5 件产品:

  • 其中 2 个使用 shipping-class-1(产品额外运费 70 美元)
  • 其中 3 个使用 shipping-class-2(产品额外运费 50 美元)

因此,如果(例如)常规运费为 120 美元,那么运费应为 ($120 + $290) = $410

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

非常感谢您的帮助。

提前致谢!

【问题讨论】:

    标签: php wordpress woocommerce shipping-method product-quantity


    【解决方案1】:

    这通常不需要任何代码......您应该将您的运输方式设置为如下(所以首先删除您的代码并保存)

    1) 因此,您将在您的运输方式中保留 120 作为全球成本。

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

    • [qty]*70shipping-class-1
    • [qty]*50shipping-class-2
    • 计算类型: Per class: Charge shipping for each shipping class individually

    这将起作用,并且会根据购物车物品数量通过切分等级来增加成本。


    对于基于您的代码的自定义运输方式,您应该使用以下方式来处理商品数量:

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

    它应该可以处理汽车项目数量的额外成本......

    不要忘记在配送设置中刷新配送方式,然后在相关配送区域中禁用/保存并重新启用/保存任何配送方式。

    其他与woocommerce_package_rates挂钩的answer threads

    【讨论】:

    • 嗨 LoicTheAztec!感谢您一如既往的快速响应。我试过你的代码,但没有任何反应:(
    • @LoikTheAztec,确实我不能使用统一费率选项,因为我使用的是专门为名为 Cologicics 的本地运输服务创建的自定义运输方式。它是由自定义程序员开发的插件,它在外部 API 的帮助下检索运输成本。这就是我找到第一个代码的原因。正如我所说,它有效,但我需要为购物车中的每件商品乘以它。我认为这更容易:(非常感谢您的宝贵时间。
    • @Voltaki 我已经根据你的重新更新了我的答案,添加了一些不同的代码......
    • @LoikTheAztec 是的!有效!你是最棒的。非常感谢。
    • @LoikTheAztec 你好,我又来了。正如我之前所说,它确实有效,但它不适用于运输方法的实例。我只需要调整一个特定实例的价格。我尝试了 woocommerce 经典的固定费率,但它也不起作用。例如,如果我设置: $shipping_method_ids = array( 'flat_rate' );它会调整配置的所有统一费率实例的价格,但是当我将其更改为:'flat_rate:5' 它停止工作并返回到原始成本。
    猜你喜欢
    • 2023-03-23
    • 2018-11-28
    • 2018-11-05
    • 2019-08-07
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多