Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter magento order collection

Tags:

magento

I use the following line to load collection of orders from magento:

// Load Order Collection
$order_collection = Mage::getModel('sales/order')->getCollection();

How do you filter this collection to ignore orders with status "canceled" and "complete"?


Update

After posting this, I was bored so I did some digging around and this post helped me find the right lines of code: http://www.magentocommerce.com/boards/v/viewthread/201797/#t287235

This is how I solved it:

// Load Order Collection
$order_collection = Mage::getModel('sales/order')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('nin' => array('canceled','complete')));
like image 657
Latheesan Avatar asked May 13 '13 14:05

Latheesan


People also ask

How can I get order details from customer ID in Magento 2?

The customer id is required to fetch the customer placed Order collection in Magento 2. Get Customer Order collection, We have to create a factory object of Magento\Sales\Model\ResourceModel\Order\Collection class. Instantiate the Factory object to the __consturct() method and apply condition with customer id.

How do I get sales order items in Magento 2?

Let's fetch the products in your Magento 2 store. /** * @var $block \MageSpark\OrderItem\Block\Item */ $itemId = 120; // Use your desired item id here $itemCollection = $block->getOrderItem($itemId); echo $itemCollection->getOrderId(); echo "<pre>"; print_r($itemCollection->debug()); That's it.

How do I create a join query in Magento 2?

Overview of joining data between 2 tables in Magento 2Step 1: Set a Collection class that extends. Step 2: Set your own function to get data. Step 3: Get the collection and call to filterOrder function above. Step 4: Save.


2 Answers

Use the addFieldToFilter method

$order_collection = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('status', array('nin' => array('canceled','complete')));
like image 71
JNDPNT Avatar answered Oct 19 '22 23:10

JNDPNT


If you want to use the original definitions:

$order_collection = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('status', array('nin' => array(
        Mage_Sales_Model_Order::STATE_NEW,
        Mage_Sales_Model_Order::STATE_CANCELED
            )));

Like defined in Mage_Sales_Model_Order:

/**
 * Order states
 */
const STATE_NEW             = 'new';
const STATE_PENDING_PAYMENT = 'pending_payment';
const STATE_PROCESSING      = 'processing';
const STATE_COMPLETE        = 'complete';
const STATE_CLOSED          = 'closed';
const STATE_CANCELED        = 'canceled';
const STATE_HOLDED          = 'holded';
const STATE_PAYMENT_REVIEW  = 'payment_review';
like image 22
Stefan Avatar answered Oct 20 '22 00:10

Stefan