英文:
Sort WooCommerce shipping options by type and by ascending cost
问题
根据 https://stackoverflow.com/questions/62570577/sort-woocommerce-shipping-options-by-ascending-cost-with-free-shipping-at-the-en/62579857#62579857 答案线程,我尝试按类型和升序成本对所有运输选项进行排序:
- 首先是免费运输选项,
- 然后是固定费率选项,
- 然后是升序成本的付费运输选项,
- 最后是本地自取选项,因为很少有客户使用它。
我无法使其工作。
任何帮助都将不胜感激。
英文:
Following https://stackoverflow.com/questions/62570577/sort-woocommerce-shipping-options-by-ascending-cost-with-free-shipping-at-the-en/62579857#62579857 answer thread, I have tried to sort all shipping options by type and by ascending cost:
- Free shipping options first,
- followed by Flat rate options,
- then Paid shipping options by ascending cost
- and last Local pickup option(s) as very few customers use it.
I haven't been able to make it work.
Any help is appreciated.
答案1
得分: 1
以下是根据此线程编写的代码,将按类型和升序成本排序的运输费率:
add_filter( 'woocommerce_package_rates', 'sort_shipping_method_by_type_and_by_asc_cost', 10, 2 );
function sort_shipping_method_by_type_and_by_asc_cost( $rates, $package ) {
if ( empty( $rates ) || ! is_array( $rates ) ) return;
// 根据成本对运输方式进行排序
uasort( $rates, function ( $a, $b ) {
if ( $a == $b ) return 0;
return ( $a->cost < $b->cost ) ? -1 : 1;
} );
$free = $flat = $local = $other = []; // 初始化
// 循环遍历运输费率
foreach ( $rates as $rate_key => $rate ) {
// 对于“免费运输”方法
if ( 'free_shipping' === $rate->method_id ) {
$free[$rate_key] = $rate;
unset($rates[$rate_key]);
}
// 对于“固定费率”
elseif ( 'flat_rate' === $rate->method_id ) {
$flat[$rate_key] = $rate;
unset($rates[$rate_key]);
}
// 对于“本地自取”
elseif ( 'local_pickup' === $rate->method_id ) {
$local[$rate_key] = $rate;
unset($rates[$rate_key]);
}
// 其他运输方式
else {
$other[$rate_key] = $rate;
unset($rates[$rate_key]);
}
}
// 合并分开的费率
return array_merge($free, $flat, $other, $local);
}
此代码应放在您的子主题的functions.php文件中(或者放在插件中)。已测试并可用。
重要提示: 您需要通过清空购物车来刷新运输方式缓存。
英文:
The following, based on this thread, will sort shipping rates by type and by ascending cost:
add_filter( 'woocommerce_package_rates' , 'sort_shipping_method_by_type_and_by_asc_cost', 10, 2 );
function sort_shipping_method_by_type_and_by_asc_cost( $rates, $package ) {
if ( empty( $rates ) || ! is_array( $rates ) ) return;
// Sort shipping methods based on cost
uasort( $rates, function ( $a, $b ) {
if ( $a == $b ) return 0;
return ( $a->cost < $b->cost ) ? -1 : 1;
} );
$free = $flat = $local = $other = []; // Initializing
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// For "Free shipping" methods
if ( 'free_shipping' === $rate->method_id ) {
$free[$rate_key] = $rate;
unset($rates[$rate_key]);
}
// For "Flat rate"
elseif ( 'flat_rate' === $rate->method_id ) {
$flat[$rate_key] = $rate;
unset($rates[$rate_key]);
}
// For "Local pickup"
elseif ( 'local_pickup' === $rate->method_id ) {
$local[$rate_key] = $rate;
unset($rates[$rate_key]);
}
// Other shipping methods
else {
$other[$rate_key] = $rate;
unset($rates[$rate_key]);
}
}
// Merge separated rates
return array_merge($free, $flat, $other, $local);
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and work.
> Inmportant: You need to refresh shipping methods cache, by emptying your cart.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论