0
votes

I have a woocommerce website and I have set 2 shipping methods:
- Flat Rate
- Local pickup

I would like to set the "Flat rate" shipping method as default (selected) in the cart or checkout page.

Any help should be appreciated.

2
where is your code,what have you tried?madalinivascu
i think you need to change selection/priority help.extensionworks.com/hc/en-us/articles/…AZinkey
You should give shopper option to choose, don't force it.Scriptonomy
Point A works for me @LoicTheAztecChandan Sahu
@ChandanSahu Ok I have removed the other unnecessary code. thanksLoicTheAztec

2 Answers

2
votes

1) You can use the following code (to set "flat rate" shipping method as default) In cart page:

add_action( 'woocommerce_before_cart', 'set_default_chosen_shipping_method', 5 );
function set_default_chosen_shipping_method(){
    //
    if( count( WC()->session->get('shipping_for_package_0')['rates'] ) > 0 ){
        foreach( WC()->session->get('shipping_for_package_0')['rates'] as $rate_id =>$rate)
            if($rate->method_id == 'flat_rate'){
                $default_rate_id = array( $rate_id );
                break;
            }

        WC()->session->set('chosen_shipping_methods', $default_rate_id );
    }
}

Code goes in function.php file of your active child theme (active theme or in any plugin file).

Tested and Works in WooCommerce 3+


2) You can also reorder the shipping rates in your shipping zones settings (but it doesn't really works as the last chosen shipping method take the hand).

0
votes

You could use the following code to set 'any' shipping method as default.

function reset_default_shipping_method( $method, $available_methods ) {
    $default_method = 'wf_fedex_woocommerce_shipping:FEDEX_GROUND'; //provide the service name here
    if( array_key_exists($method, $available_methods ) )
        return $default_method;
    else
        return $method;
}

Let's say, you're using a Carrier shipping plugin like WooCommerce FedEx Shipping Plugin. You can fetch the value Id (shown below) and paste it under the '$default_method' in the above code.

You will have to copy and paste the code in WordPress Dashboard->Appearance–>Editor–>functions.php of your theme.

Hope that helped. :)

copy the value Id from here