這一過程需要2步:一些代碼和一些設置...
1)代碼 - 您可以使用woocommerce_package_rates
過濾鉤子鉤住一個自定義功能,當車項目是針對「固定費率」航運法從1個多供應商:
add_filter('woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2);
function custom_flat_rate_cost_calculation($rates, $package)
{
// SET BELOW your attribute slug… always begins by "pa_"
$attribute_slug = 'pa_vendor'; // (like for "Color" attribute the slug is "pa_color")
// Iterating through each cart item to get the number of different vendors
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
// The attribute value for the current cart item
$attr_value = $cart_item[ 'data' ]->get_attribute($attribute_slug);
// We store the values in an array: Each different value will be stored only one time
$attribute_values[ $attr_value ] = $attr_value;
}
// We count the "different" attribute values stored
$count = count($attribute_values);
// Iterating through each shipping rate
foreach($rates as $rate_key => $rate_values){
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
// Targeting "Flat Rate" shipping method
if ('flat_rate' === $method_id) {
// For more than 1 vendor (count)
if($count > 1){
// Get the original rate cost
$orig_cost = $rates[$rate_id]->cost;
// Calculate the new rate cost
$new_cost = $orig_cost + 20; // 19 + 20 = 39
// Set the new rate cost
$rates[$rate_id]->cost = $new_cost;
// Calculate the conversion rate (for below taxes)
$conversion_rate = $new_cost/$orig_cost;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_id]->taxes as $key => $tax){
if($rates[$rate_id]->taxes[$key] > 0){
$new_tax_cost = number_format($rates[$rate_id]->taxes[$key]*$conversion_rate, 2);
$rates[$rate_id]->taxes[$key] = $new_tax_cost; // set the cost
}
}
}
}
}
return $rates;
}
代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。
該代碼與woocommerce版本測試3+和工作
2)設置, - 一旦上面的代碼已經被保存在您的活動主題的function.php文件,你將需要設置(對於您所有的運輸區域)「統一費率」運輸方式的費用爲19
(£19)(並保存)。
重要:要刷新配送方式緩存,你需要禁用「扁平率」則保存,並使背部「扁平率」則保存。
現在,這應該適合您的預期。
完美地工作。那是@LoicTheAztec - 非常感謝。 –