Magento – How to get orders collection in magento 2

collection;controllersmagento2orders

Magento 2 get order collection to update customers id using csv.

For example we can take magento 1 we can update using test.php file like that how we can update in magento 2.

I am creating new module I am planing to update using controller.But in that how can i get orders collection can any one help me….

Best Answer

Magento 2 get order collection to update customers id using csv.

For example we can take magento 1 we can update using test.php file like that how we can update in magento 2.

I have no idea what you want to tell us here, but I can answer the question:

I am creating new module I am planing to update using controller.But in that how can i get orders collection

Collections are now a mere implementation detail, you work with them using SearchCriteria and Repository objects.

First, inject the OrderRepository and the SearchCriteriaBuilder into your controller, by adding it to the constructor parameters:

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
    $this->orderRepository = $orderRepository;
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    parent::__construct(
        $context
    );
}

Then you can use it like this:

$criteria = $this->searchCriteriaBuilder
//  ->addSortOrder(...)
//  ->addFilter(...)
//  ...
    ->create();
$orderResult = $this->orderRepository->getList($criteria);
$orders = $orderResult->getItems();
Related Topic