Title: How to change default shipping method programmatically
Last modified: August 31, 2016

---

# How to change default shipping method programmatically

 *  Resolved [SJW](https://wordpress.org/support/users/whitsey/)
 * (@whitsey)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/)
 * WooCommerce cant prioritise table rate shipping methods so I am trying to do 
   it myself but am failing horribly.
 * I have tried to add an action at the point I think it is set but it is not working.
   Here is what I have tried (I just want to use the first shipping in the available
   method list):
 *     ```
       function reset_default_shipping_method() {
   
       	$available_methods = WC()->shipping->packages[0]['rates'];
       	$chosen_method = key($available_methods);
       	WC()->session->set( $chosen_method );
   
       }
       add_action('woocommerce_shipping_method_chosen', 'reset_default_shipping_method');
       ```
   
 * I have looked at the session after this and the session hasn’t changed BUT, when
   I run the function attached to a different action, I can see that all the variables
   are being set correctly (except I can’t confirm `WC()->session->set( $chosen_method);`)
 * I need to be updating the session before page load BUT it needs to be done in
   a way that if it is manually overridden by the customer, then don’t change it
   back.
 * I have been working on this for the past 4 hours and don’t feel any closer to
   a solution. Hoping someone here can help out.
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/#post-7221135)
 * Assuming each rate has a unique ID, you can set the default here via a filter:
   [https://github.com/woothemes/woocommerce/blob/fa30a38c58373d9c3706cc0b7ae22032de3a2985/includes/class-wc-shipping.php#L283](https://github.com/woothemes/woocommerce/blob/fa30a38c58373d9c3706cc0b7ae22032de3a2985/includes/class-wc-shipping.php#L283)
 *  Thread Starter [SJW](https://wordpress.org/support/users/whitsey/)
 * (@whitsey)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/#post-7221223)
 * For those of you also searching, here is the result I ended up with that works
   to my requirements.
 * WooCommerce reset default shipping method:
 *     ```
       /*=Use the shipping method filter to set the "selected" shipping method to
        * use the first (default) method in the available methods list
       **************************************************************************/
       function my_reset_default_shipping_method( $method, $available_methods ) {
   
       	// get the id of the first method in the list
       	$method = key($available_methods);
       	return $method;
   
       }
       add_filter('woocommerce_shipping_chosen_method', 'my_reset_default_shipping_method', 10, 2);
       ```
   
 *  [unicco](https://wordpress.org/support/users/unicco/)
 * (@unicco)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/#post-8167827)
 * I were expericing a problem, where the system by default selected local pickup
   ~ free. This is due to the fact, that the get_default_method returns the first
   id, which in my case, was the local pickup. I wanted a more intelligent solution,
   as the majority of my customers choose an delivery-option indifferent from the
   local pickup. I wanted the cheapest shipping to be selected as default, and ignore
   the local pickup.
 *     ```
       function ea_default_shipping_method( $method ) {
           $_cheapest_cost = 100000;
       	$packages = WC()->shipping()->get_packages()[0]['rates'];
   
       	foreach ( array_keys( $packages ) as $key ) {
               if ( ( $packages[$key]->cost > 0 ) && ( $packages[$key]->cost < $_cheapest_cost ) ) {
                   $_cheapest_cost = $packages[$key]->cost;
                   $method_id = $packages[$key]->id;
               }
       	}
   
       	return $method_id;
       }
       add_filter( 'woocommerce_shipping_chosen_method', 'ea_default_shipping_method', 10 );
       ```
   
 *  [bo3lih](https://wordpress.org/support/users/bo3lih/)
 * (@bo3lih)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/#post-8604939)
 * Hi [@unicco](https://wordpress.org/support/users/unicco/) , Thanks for the code!
   it worked flawlessly 🙂
    -  This reply was modified 9 years, 5 months ago by [bo3lih](https://wordpress.org/support/users/bo3lih/).
 *  [73631420](https://wordpress.org/support/users/73631420-1/)
 * (@73631420-1)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/#post-8629095)
 * Hey [@unicco](https://wordpress.org/support/users/unicco/)!
    Where would I put
   your code snippet to make it work? Thanks
 * Florian
    -  This reply was modified 9 years, 5 months ago by [73631420](https://wordpress.org/support/users/73631420-1/).
 *  [Peter Lawrenson](https://wordpress.org/support/users/lorro/)
 * (@lorro)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/#post-8630797)
 * [@unicco](https://wordpress.org/support/users/unicco/)
    In functions.php in your
   child theme.
 *  [73631420](https://wordpress.org/support/users/73631420-1/)
 * (@73631420-1)
 * [9 years, 5 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/#post-8631476)
 * Yepp that did it! Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘How to change default shipping method programmatically’ is closed to 
new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 7 replies
 * 6 participants
 * Last reply from: [73631420](https://wordpress.org/support/users/73631420-1/)
 * Last activity: [9 years, 5 months ago](https://wordpress.org/support/topic/how-to-change-default-shipping-method-programmatically/#post-8631476)
 * Status: resolved