简体   繁体   中英

List countries and shipping cost in woocommerce

i want to return an array of countries with their shipping price in php.

I already have the country list with:

$countryClass = new WC_Countries();
$countryList = $countryClass->get_shipping_countries();

But it seems not possible to find the shipping cost with:

$shipping = new WC_Shipping();
$shippingMethods = $shipping->get_shipping_methods(true);

Anyone had this problem or found the issues?

Found the answers, if this can help someone somehow

function getShippingCountry(){
$countryClass = new WC_Countries();
$countryList = $countryClass->get_shipping_countries();

$shippingZones = new WC_Shipping_Zones();
$deliveryZones = $shippingZones->get_zones();

$array = [];

foreach ($deliveryZones as $zone){

    if(isset($countryList[$zone['zone_locations'][0]->code])){
        foreach ($zone['shipping_methods'] as $shippingMethod){
            if(!isset($array[$zone['zone_name']]) || $array[$zone['zone_name']] < $shippingMethod->instance_settings['cost'] )
            $array[$zone['zone_name']] = $shippingMethod->instance_settings['cost'];
        }
    }
}

return $array;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM