为什么不适用于默认运输方式?对于 WooCommerce

Why does not work for default shipping method? For WooCommerce

我要默认DHL。但它不起作用,这是为什么?在 WordPress 的 WooCommerce 中。

您可以在这里看到:img。方式 - functions.php

function reset_default_shipping_method( $method, $available_methods ) {

    if ( ! empty( $method ) ) {
        return $method;
    }        

    $method = 'shipping_method_0_flat_rate9';

    return $method;
}

add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 2);
function reset_default_shipping_method( $method, $available_methods ) {
    $default_method = 'wf_fedex_woocommerce_shipping:FEDEX_GROUND'; //provide here the service name which will selected default
    if( array_key_exists($method, $available_methods ) )
        return $default_method;
    else
        return $method;
}
add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 2);

将此添加到您的主题 functions.php 更改 $default_method var.

来源:https://www.xadapter.com/set-default-shipping-method-woocommerce/

过滤器已更新,您应该试试这个。 我已经测试过了,它对我有用。

此外,默认运送存储在您的会话中,因此如果您想测试它,您可以尝试从新浏览器或注销并重新登录。

 function prefix_reset_default_shipping_method( $default, $package, $chosen_method ) {

        if( $default !== 'flat_rate:9')
            $default = 'flat_rate:9';

        return $default;

    }

    add_filter('woocommerce_shipping_chosen_method', 'prefix_reset_default_shipping_method', 10, 3);