Woocommerce 运费基于特定运费的商品数量 class

Woocommerce shipping cost based on item quantity for specific shipping class

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

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

因此,如果(例如)常规运费为 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;
    }
}

非常感谢您的帮助。

提前致谢!

这通常不需要任何代码......您应该按照以下方式设置您的送货方式(所以首先删除您的代码并保存):

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

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

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

这会奏效,并且会根据购物车商品数量增加 class 的费用。


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

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

它应该可以处理汽车物品数量的额外费用……

Don't forget to refresh shipping methods going to shipping settings, then disable/save and re-enable/save any shipping method in the related shipping zone.

其他 answer threadswoocommerce_package_rates 挂钩有关。