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.
- The topic ‘How to change default shipping method programmatically’ is closed to new replies.