Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WooCommerce] 運送方法項目的排序控制(指定順序)
function mxp_sort_shipping_methods_order($available_shipping_methods, $package) {
if (!$available_shipping_methods) {
return;
}
// 運送方式,檢視 radio 元素,找到的 value 值
$sort_order = array(
'flat_rate:1' => null,
'some_other_shipping_method_slug:3' => null,
'flat_rate:2' => null,
);
$others = array();
// 拆解
foreach ($available_shipping_methods as $shipping_slug => $shipping_obj) {
if (array_key_exists($shipping_slug, $sort_order)) {
$sort_order[$shipping_slug] = $available_shipping_methods[$shipping_slug];
} else {
$others[$shipping_slug] = $available_shipping_methods[$shipping_slug];
}
}
// 重組合併
$sort_order += $others;
foreach ($sort_order as $id => $null_obj) {
if (empty($null_obj)) {
// 去除不存在的選項
unset($sort_order[$id]);
}
}
return $sort_order;
}
add_filter('woocommerce_package_rates', 'mxp_sort_shipping_methods_order', 11, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.