With the code below, I am able to change the display of specific shipping methods full labels on WooCommerce cart and checkout pages:
add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_labels', 10000, 2 );
function custom_shipping_labels($label, $method){
$shpmethod = $label;
if(strpos($shpmethod, 'Express Shipping') !== false){
$shpmethod = str_replace('Express Shipping',' test express lbl',$shpmethod);
}
elseif(strpos($shpmethod, 'Free Standard Shipping') !== false){
$shpmethod = str_replace('Free Standard Shipping',' test free lbl',$shpmethod);
}
return $shpmethod;
}
Now I need to access the customer selected county inside that custom function hooked in woocommerce_cart_shipping_method_full_label
filter hook.
Is it possible to get the customer selected country in that hooked function?
2
Answers
For that you can use
WC_Customer::get_shipping_country()
To get the selected shipping country you can use one of those:
WC()->session->get('customer')['shipping_country']
.WC()->checkout->get_value('shipping_country')
WC()->customer->get_shipping_country()
.For testing purpose, here with the code below, selected (or customer) billing country code is displayed with each shipping method full label:
Now in your code you can use
$method->label
to target each defined shipping method label string, which is more efficient. So your code will be:Code goes in functions.php file of your active child theme (active theme). Tested and works.
Now you can use the selected shipping country as you wish.