I try to get a shipping ID list by country like flat_rate:1, local_pickup:2 etc.
I tried this below
$all_zones = WC_Shipping_Zones::get_zones();
$country_code = 'BE';
foreach ($all_zones as $zone) {
foreach ($zone['zone_locations'] as $location) {
if ($country_code === $location->code) {
foreach ($zone['shipping_methods'] as $flat_rate) {
print_r($flat_rate);//ID here
}
}
}
}
I tried also this but I can not set a country by myself?
$shippingmethods = WC()->session->get( 'shipping_for_package_0')['rates'];
foreach ($shippingmethods as $shippingmethod ) {
shippingmethod->get_id();
}
2
Answers
The following will give you the shipping rate Id that you are looking for… It works for all defined shipping zones and also Rest of the World shipping zone (when no country matches).
The code:
Tested and work.
Changed this line $method_id = $shipping_method->method_id; to $shipping_method->id;
This fixed the issue Notice: Undefined property: WC_Shipping_Flat_Rate::$method_id
Tested and work