Get country from shipping address in core commerce

Permalink
I've built a new shipping cost module which supplies cost depending on country.

In my controller.php, in function getAvailableShippingMethods($currentOrder) I need to retrieve the country code (eg. 'GB') and though to do it with

$country = $currentOrder->getAttribute('shipping_address')->getCountry();


however it seems that $currentOrder->getAttribute('shipping_address') returns a text string of shipping address and not an address object and so getCountry() fails.

How should I be doing this? I'm sure I'm missing something obvious! Probably I should be using something other than getAttribute, but I can't see what.

 
trentham replied on at Permalink Best Answer Reply
I THINK I've sorted this!

It seemed that because I'd used 'deliver to billing address', the shipping address didn't return the required object and under those circumstances I need to use the billing address to get the country.

$addr = $currentOrder->getAttribute('shipping_address');
if (!$addr)      $addr = $currentOrder->getAttribute('billing_address');
$country = $addr->getCountry();


It seems to be working.