1

How can I get a list of all the orders in processing and pending status in Magento 2?

1

We should use Service Contracts Layer:

class OrderCollectData
{
    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $orderRepository;
    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
     * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
     */
    public function __construct(
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        $this->orderRepository = $orderRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }

    public function execute()
    {
        $searchCriteria = $this->searchCriteriaBuilder
        ->addFilter(
           'status'
           'processing'
           'eq'
        )
        ->addFilter(
           'status'
           'pending'
           'eq'
        )->create();

        $orders = $this->orderRepository->getList($searchCriteria);
        foreach ($orders->getItems() as $order) {
               //Your code
        };

    }
}
0

Something like this should work

public function __construct(
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
) {
    $this->orderCollectionFactory = $orderCollectionFactory;
}

public function getOrderCollection($status)
{
    $orderCollection = $this->orderCollectionFactory
        ->create()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status', ['eq'=> $status]);

    return $orderCollection;
}

So you would call

$this->getOrderCollection('processing')
$this->getOrderCollection('pending')

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.