1 回答

TA貢獻2012條經驗 獲得超12個贊
這通常不需要任何代碼......您應該將您的運輸方式設置如下(所以首先刪除您的代碼并保存):
1) 因此,您將120
在運輸方式中保留全球成本。
2)然后對于每個相關的所需運輸類別,您將在相應的字段中添加:
[qty]*70
為了shipping-class-1
[qty]*50
為了shipping-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
- 1 回答
- 0 關注
- 134 瀏覽
添加回答
舉報