Normally this can be done in your WooCommerce shipping settings. You can sort shipping methods by dragging the little icon located on the left of each shipping method:
这在大多数情况下都应该有效.
You can do it also programmatically.
1) Finding the correct shipping method keys
首先,由于您没有在代码中使用the right shipping methods key reference,因此要查找正确的发货方法关键字引用,请使用以下代码:
add_action('wp_footer', 'wp_footer_test_output', 10);
function wp_footer_test_output() {
if( current_user_can('administrator') ) {
echo '<pre>';
foreach ( WC()->session->get('shipping_for_package_0')['rates'] as $rate ) {
printf('For this <strong>"%s"</strong> shipping method, the key is: <code>%s</code><br>', $rate->label, $rate->id);
}
echo '</pre>';
}
}
然后在您的网站中,将产品添加到购物车并转到购物车页面.在页脚上,您将看到显示了您的所有活动发货方法及其参考代码.
2) Sorting displayed shipping methods
获得所需送货方式的正确密钥后,即可删除代码并使用下面的送货方式密钥.
以下代码将首先移动所需的显示运输方式:
add_filter('woocommerce_package_rates', 'sort_shipping_methods', 10, 2);
function sort_shipping_methods($rates, $package) {
// Here define the correct shipping method key
$shipping_method_key = 'flat_rate:9';
if (isset($rates[$shipping_method_key])) {
$rates1[$shipping_method_key] = $rates[$shipping_method_key];
unset($rates[$shipping_method_key]);
}
return isset($rates1) ? array_merge($rates1, $rates) : $rates;
}
代码位于您的子主题的unctions.php文件中(或在插件中).经过测试和工作.
Important:您必须清空购物车才能刷新发货方法缓存.
相关帖子: