1
votes

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?

2

2 Answers

6
votes

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.

-1
votes

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