Question

I would like to filter out my home country (GB or United Kingdom) from the following sales/order collection. How can this be done?

$orders = Mage::getModel('sales/order')->getCollection()            
        ->addFieldToSelect('entity_id')
        ->addFieldToFilter('status', 'complete')
        ->setOrder('created_at', 'desc')
            ->setPageSize(40); // Last 40 Orders
Was it helpful?

Solution

Looks like the query below may do the trick for you:

 /** @var Mage_Core_Model_Resource $resource
*/
$resource = Mage::getSingleton('core/resource');
$orders = Mage::getModel('sales/order')->getCollection()
                ->addFieldToSelect('entity_id')
                ->addFieldToFilter('status', 'complete')
                ->setOrder('created_at', 'desc')
                ->setPageSize(40); // Last 40 Orders

$orders->join('order_address',
                'main_table.entity_id=order_address.parent_id',
                ['address_type', 'country_id']);
$orders->addAttributeToFilter('order_address.address_type', 'shipping');
$orders->addAttributeToFilter('order_address.country_id', 'GB');

OTHER TIPS

you can get the Shipping address and Customer data from the Order collection. from that, you can easily get the country name/id.

Because you can not directly filter the sales/order collection by country. you need to build your custom code for that.

Here is the code you can use to get the order shipping address/ billing address details

$incrementid ="100000004";
$_order = Mage::getModel('sales/order')->loadByIncrementId($incrementid);

OR

$order_id ="4";
$_order = Mage::getModel('sales/order')->load($order_id);

$_shippingAddress = $_order->getShippingAddress();
echo $_shippingAddress->getCountry_id();

Hope it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top