WooCommerce cannot 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 method in the available shipping methods array):
function reset_default_shipping_method() {
$available_methods = WC()->shipping->packages[0]['rates'];
$chosen_method = key($available_methods);
WC()->session->set( 'chosen_shipping_methods', $chosen_method );
}
add_action('woocommerce_shipping_method_chosen', 'reset_default_shipping_method');
I have tested $available_methods and $chosen_method and both are presenting as expected (when run in page) BUT, once I add it to the action woocommerce_shipping_method_chosen it doesn't seem to be updating.
e.g. If I run this through a different hook:
function output_to_footer() {
if ( current_user_can( 'administrator' ) ) :
$current_method = WC()->session->get( 'chosen_shipping_methods');
$available_methods = WC()->shipping->packages[0]['rates'];
$chosen_method = key($available_methods);
WC()->session->set( 'chosen_shipping_methods', $chosen_method );
print "\n".'-----'."\n";
print_r($current_method);
print "\n".'-----'."\n";
print_r($available_methods);
print "\n".'-----'."\n";
print_r($chosen_method);
print "\n".'-----'."\n";
endif;
}
add_action('print_footer_messages', 'output_to_footer');
It looks like everything is doing what it should, but when I run it from the action : woocommerce_shipping_method_chosen it "appears" to do everything BUT the shipping radio is still set to the old shipping method?
-----
Array
(
[0] => table_rate-5 : 70
)
-----
Array
(
[table_rate-7 : 72] => WC_Shipping_Rate Object
(
[id] => table_rate-7 : 72
[label] => Registered Australian Post (2 to 8 Business Days)
[cost] => 4.0909
[taxes] => Array
(
[1] => 0.40909
)
[method_id] => table_rate
)
[table_rate-8 : 90] => WC_Shipping_Rate Object
(
[id] => table_rate-8 : 90
[label] => Tracking and Freight Insurance
[cost] => 20.055881818182
[taxes] => Array
(
[1] => 2.0055881818182
)
[method_id] => table_rate
)
[table_rate-5 : 70] => WC_Shipping_Rate Object
(
[id] => table_rate-5 : 70
[label] => Nation-Wide Delivery (5 to 12 Business Days)
[cost] => 0
[taxes] => Array
(
)
[method_id] => table_rate
)
[local_pickup] => WC_Shipping_Rate Object
(
[id] => local_pickup
[label] => Local Pickup
[cost] => 0
[taxes] => Array
(
)
[method_id] => local_pickup
)
)
-----
table_rate-7 : 72
-----
I have been working on this all day and don't feel any closer to a solution. Hoping someone here can help out.
woocommerce_shipping_chosen_method is a filter hook not an action hook. Try the following code
function reset_default_shipping_method( $method, $available_methods ) {
// If the shipping method has been chosen don't do anything
if ( ! empty( $method ) ) {
return $method;
}
// add code to set 'Table Rate' as the default shipping method
$method = 'table_rate-5';
return $method;
}
add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 2);
P.S: I have hardcoded table_rate-5 as the default method to give you the gist, so change it to the desired one. Ideally you need to write up code to see if the method you are wanting to set as default is available in $available_methods and then work accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With