按成本升序对 WooCommerce 运输选项进行排序,最后免费送货
Sort WooCommerce shipping options by ascending cost with free shipping at the end
我正在尝试对 woocommerce 运输选项进行排序。我想按从低到高的价格排序,但我最后需要免运费选项。
我在 WooCommerce: Sort Shipping Costs from Low to High
工作
如何把 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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
现在同样的事情**零成本和空成本先**:
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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
我正在尝试对 woocommerce 运输选项进行排序。我想按从低到高的价格排序,但我最后需要免运费选项。
我在 WooCommerce: Sort Shipping Costs from Low to High
工作如何把 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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
现在同样的事情**零成本和空成本先**:
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;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.