更改Woocommerce中的默认送货方式 [英] Change the default Shipping method in Woocommerce

查看:50
本文介绍了更改Woocommerce中的默认送货方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种送货方式.首先是免费送货,其次是特快专递的固定费用送货,为此我要收取额外费用.默认情况下,购物车中选择了快递",这导致一些买家感到困惑,因为我不提供免费送货服务.

I have two shipping methods. First is Free Shipping and second is Flat Rate Shipping for Express shipping for which i charge extra fee. By default Express Shipping is selected in the cart which lead to confusion among some buyers that I do not offer free shipping.

是否可以将默认的选定方法更改为免费送货?

Is it possible to change default selected method to free shipping?

推荐答案

我认为您只需要重新订购每个送货区域的送货方式,就可以免费送货"了.在第一行.

I think that you just need to reorder your shipping methods for each shipping zone, moving "free shipping" on first line.

如果它不起作用,则可以添加以下代码:

If it doesn't work, you can add the following code:

add_action( 'woocommerce_before_cart', 'auto_select_free_shipping_by_default' );
function auto_select_free_shipping_by_default() {
    if ( isset(WC()->session) && ! WC()->session->has_session() )
        WC()->session->set_customer_session_cookie( true );

    // Check if "free shipping" is already set
    if ( strpos( WC()->session->get('chosen_shipping_methods')[0], 'free_shipping' ) !== false )
        return;

    // Loop through shipping methods
    foreach( WC()->session->get('shipping_for_package_0')['rates'] as $key => $rate ){
        if( $rate->method_id === 'free_shipping' ){
            // Set "Free shipping" method
            WC()->session->set( 'chosen_shipping_methods', array($rate->id) );
            return;
        }
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.

Code goes in function.php file of your active child theme (or active theme). tested and works.

如果不使用购物车页面,并且存在重定向至结帐的功能,则必须将 woocommerce_before_cart 替换为 woocommerce_before_checkout_form 钩子在代码中.

If you don't use a Cart page and there is a redirection to checkout, you will have to replace woocommerce_before_cart by woocommerce_before_checkout_form hook in the code.

这篇关于更改Woocommerce中的默认送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆