1

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.

3

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.

  • Call to undefined method Magento\Sales\Model\ResourceModel\Order\Collection::addFieldToField() – Arshad Hussain Apr 4 at 13:05
  • sorry typo erro check now – Prathap Gunasekaran Apr 4 at 13:06
  • I have checked it in db for status, its 'canceled' but still it is not working niether yours nor mine. – Arshad Hussain Apr 5 at 10:42
  • Will it returns collection – Prathap Gunasekaran Apr 5 at 10:44
  • It is returning collection including all types of status – Arshad Hussain Apr 5 at 10:45
2

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;
    }
}

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.