• Resolved rasidrsd

    (@rasidrsd)


    Hey guys,

    i have set three shipping zones: Germany, Europe, Rest of World.
    How can I access the exact names?

    I have tried to get it with
    $zone= WC_Shipping_Zones::get_zones();
    and there is a string called ‘zone_name’ but how can I get this?

    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Your line of code will return an array, so you need to step through it and get the name for each zone:

    
    $zones = WC_Shipping_Zones::get_zones();
    // test $zones is an array
    foreach ( $zones as $zone ) {
      echo $zone['zone_name'];
    }
    

    Sorry, not tested.

    Thread Starter rasidrsd

    (@rasidrsd)

    I have tried your code, it works perfectly, but I noticed my assumption was wrong.
    get_zones has all possible zones, not only the selected one (by selected I mean that when the customer enters his adress, it selects the zone automatically (tested, works)).

    How can I get just the selected Zone?

    Get the order, then get the method_id for that order. The zone id is part of the method_id, the other part being “flat-rate” or whatever.

    Thread Starter rasidrsd

    (@rasidrsd)

    Hey lorro,

    thank you for your help.

    I did it like this:

    $shipping_items = $order->get_items( 'shipping' );		
    	foreach($shipping_items as $rsd){
      	$order_shipping_method_id = $rsd['method_id'] ;			
    	}
    

    And then later:

    if(strpos($order_shipping_method_id, "flat_rate:1")!==false){
    //do something
    }

    Thank you.

    • This reply was modified 8 years, 1 month ago by rasidrsd.
    Thread Starter rasidrsd

    (@rasidrsd)

    Hey lorro,

    thank you for your help.

    I did it like this:

    $shipping_items = $order->get_items( 'shipping' );		
    	foreach($shipping_items as $rsd){
      	$order_shipping_method_id = $rsd['method_id'] ;			
    	}
    

    And then later:

    if(strpos($order_shipping_method_id, "flat_rate:1")!==false){
    //do something
    }

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get Zone Name’ is closed to new replies.