按成本升序排序WooCommerce运输选项,最后免费送货



我正在尝试对woocommerce运输选项进行排序。我想从最低到最高价格排序,但我需要最后的免费送货选项。

我在WooCommerce工作:从低到高对运输成本进行排序

如何将 0 放在最后?

add_filter( 'woocommerce_package_rates' , 'businessbloomer_sort_shipping_methods', 10, 2 );

function businessbloomer_sort_shipping_methods( $rates, $package ) {

if ( empty( $rates ) ) return;

if ( ! is_array( $rates ) ) return;

uasort( $rates, function ( $a, $b ) { 
if ( $a == $b ) return 0;
return ( $a->cost < $b->cost ) ? -1 : 1; 
} );

return $rates;

// NOTE: BEFORE TESTING EMPTY YOUR CART

}

以下内容将按成本从低到高对运输选项进行排序,最后是零成本和空成本。

add_filter( 'woocommerce_package_rates' , 'sort_shipping_method_by_cost_zero_empty_cost_last', 10, 2 );
function sort_shipping_method_by_cost_zero_empty_cost_last( $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 = $zero = []; // Initializing
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// For "free shipping" methods
if ( 'free_shipping' === $rate->method_id ) {
// set them on a separated array
$free[$rate_key] = $rate;
// Remove "Free shipping" method from $rates array
unset($rates[$rate_key]);
} 
// For other shipping rates with zero cost
elseif ( $rate->cost == 0 ) {
// set them on a separated array
$zero[$rate_key] = $rate;
// Remove the current method from $rates array
unset($rates[$rate_key]);
}
}
// Merge zero cost and "free shipping" methods at the end if they exist
return ! empty( $free ) || ! empty( $zero ) ? array_merge($rates, $zero, $free) : $rates;
}

代码进入活动子主题(或活动主题(的函数.php文件。经过测试并工作。

刷新发货缓存:
1(。此代码已保存在函数.php文件中。
2(. 在配送区域设置中,禁用/保存任何配送方式,然后启用返回/保存。
您已完成,可以对其进行测试。


现在同样的事情**首先是零成本和空成本**:

add_filter( 'woocommerce_package_rates' , 'sort_shipping_method_by_cost_empty_zero_cost_first', 10, 2 );
function sort_shipping_method_by_cost_empty_zero_cost_first( $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 = $zero = []; // Initializing
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// For "free shipping" methods
if ( 'free_shipping' === $rate->method_id ) {
// set them on a separated array
$free[$rate_key] = $rate;
// Remove "Free shipping" method from $rates array
unset($rates[$rate_key]);
} 
// For other shipping rates with zero cost
elseif ( $rate->cost == 0 ) {
// set them on a separated array
$zero[$rate_key] = $rate;
// Remove the current method from $rates array
unset($rates[$rate_key]);
}
}
// Merge zero cost and "free shipping" methods at the end if they exist
return ! empty( $free ) || ! empty( $zero ) ? array_merge($free, $zero, $rates) : $rates;
}

代码进入活动子主题(或活动主题(的函数.php文件。经过测试并工作。

刷新发货缓存:
1(。此代码已保存在函数.php文件中。
2(. 在配送区域设置中,禁用/保存任何配送方式,然后启用返回/保存。
您已完成,可以对其进行测试。

相关内容

最新更新