0

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
  • the order collection is having data about customers' orders. Then against these, we have customers' address, customer shipping address. So your question leaves a lot of space for ambiguity: do you mind to remove any ambiguity and I'll give it a go, thanks – Herve Tribouilloy Dec 30 '18 at 14:17
  • Wanted to filter out any UK orders at the query level Have tried ->joinAttribute('shipping_country', 'order_address/country_id', 'shipping_address_id', null, 'left') ->addAttributeToFilter('shipping_country', array('neq' => 'UK')); But was struggling to get this to work. – hejhog Jan 2 at 19:05
1

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');
  • Did this resolve your issue @hejhog – Herve Tribouilloy Jan 15 at 21:13
  • Thanks for your answer - perfect. Just for reference I wanted non-GB therefore changed to $orders->addAttributeToFilter('order_address.country_id', array('neq' => 'GB')); – hejhog Jul 3 at 22:26
  • thanks for the update, appreciated – Herve Tribouilloy Jul 4 at 12:56
0

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.

  • Thanks but I need to filter out the UK orders at the collection. See comment above. – hejhog Jan 2 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.