I'm trying to add estimated delivery times to orders on the checkout page, under the shipping price within WooCommerce.
I have worked out how to do this, such as:
add_action( 'woocommerce_after_shipping_rate', 'blm_action_after_shipping_rate', 20, 2 );
function blm_action_after_shipping_rate ( $method, $index ) {
if( is_cart() ) {
return; // Exit on cart page
}
// Use $method->method_id in here to check delivery option
}
Now, to be able to estimate the days (unfortunately the shipping API doesn't return this data) I need to find out the shipping country and shipping state/province in here - is there a way to do that?
The shipping rates are calculated from customer shipping location.
So you asked for the customer shipping country and state that you can get from WC_Customer
object using dedicated methods as follows:
add_action( 'woocommerce_after_shipping_rate', 'blm_action_after_shipping_rate', 20, 2 );
function blm_action_after_shipping_rate ( $method, $index ) {
if( is_cart() ) {
return; // Exit on cart page
}
$shipping_country = WC()->customer->get_shipping_country();
$shipping_state = WC()->customer->get_shipping_state();
// Testing output
echo '<br><small>Country code:' . $shipping_country . ' | State code: ' . $shipping_state . '</small>';
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
If customer change of country and state, the data is refreshed and the new country and state are set in WC_Customer
Object.