Set “flat rate” shipping method as default in woocommerce

前端 未结 2 1200
耶瑟儿~
耶瑟儿~ 2021-01-28 16:09

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 (sel

相关标签:
2条回答
  • 2021-01-28 16:41

    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 讨论(0)
  • 2021-01-28 16:53

    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

    0 讨论(0)
提交回复
热议问题