Question

I have got order collection with the following code:

$orders = $objectManager->create('Magento\Sales\Model\Order')->getCollection();

Which is working fine. Problem is that it returns all types of orders including 'processing', 'cancelled' etc. I don't want the cancelled order in this collection.

I don't know how to exclude cancelled order in the above code.

Hope somebody helps.

Was it helpful?

Solution

Try this,

$orders = $objectManager->create('Magento\Sales\Model\Order')->getCollection();
$orders->addFieldToSelect('*')->addFieldToFilter('status', array('in' => array(complete,processing)));

Status not in cancelled order will get into the collection.

am not sure about the status name cancelled make sure you use the exact one in the table sales_order table.

OTHER TIPS

It isn't the best way to get collections in M2 using object manager. Use collection factorty for this purpose instead. Here's a short example:

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;

class SomeClass
{
    ...
    public function __construct(
        ...
        CollectionFactory $orderCollectionFactory
        ...
    ) {
        ...
        $this->collectionFactory = $orderCollectionFactory;
        ...
    }
    ...
     public function getOrders(){
         $orders = $this->collectionFactory()->create()
             ->addFieldToSelect('*')
             ->addFieldToFilter('status', ['neq' => 'cancelled']);
         return $orders;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top