1 回答
TA贡献1829条经验 获得超7个赞
以下代码将为购物车中找到的每个附加产品类别添加额外的运费(因此不适用于第一个):
add_filter( 'woocommerce_package_rates', 'filter_shipping_rates_costs', 10, 2 );
function filter_shipping_rates_costs( $rates, $package ) {
$step_cost = 50;
$term_ids = array();
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ){
$term_ids = array_merge(
$term_ids,
(array) $cart_item['data']->get_category_ids()
);
}
$terms_count = count( $term_ids );
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id && $terms_count > 1 ) {
// Set rate cost
$rates[$rate_key]->cost = $rate->cost + ($step_cost * ($terms_count - 1));
}
}
return $rates;
}
现在,如果您想为购物车中找到的每个类别添加额外的运费,请使用以下命令:
add_filter( 'woocommerce_package_rates', 'filter_shipping_rates_costs', 10, 2 );
function filter_shipping_rates_costs( $rates, $package ) {
$step_cost = 50;
$term_ids = array();
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ){
$term_ids = array_merge(
$term_ids,
(array) $cart_item['data']->get_category_ids()
);
}
$terms_count = count( $term_ids );
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// Excluding free shipping methods
if ( 'free_shipping' !== $rate->method_id && $terms_count > 0 ) {
// Set rate cost
$rates[$rate_key]->cost = $rate->cost + ($step_cost * $terms_count);
}
}
return $rates;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
刷新运输缓存:
此代码已保存在您的functions.php 文件中。
在运输区域设置中,禁用/保存任何运输方式,然后启用返回/保存。
你已经完成了,你可以测试它。
- 1 回答
- 0 关注
- 70 浏览
添加回答
举报