Change the default Shipping method in Woocommerce

安稳与你 提交于 2021-01-29 14:26:50

问题


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?


回答1:


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;
        }
    }
}

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

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.




回答2:


function test_default_shipping_method($default,$available){ $default_method = 'wcv_pro_vendor_shipping'; //provide here the service name which will selected default if( array_key_exists($method, $available_methods ) ) return $default_method; else return $default_method; }



来源:https://stackoverflow.com/questions/53480420/change-the-default-shipping-method-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!