0

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....

5

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();
  • I'm not able to use this in order beforeSave plugin. what could be the issue? – saiid Apr 14 '18 at 12:39

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.