Magento 2.1.1 How to load Order with Increment ID using OrderRepository object

  • What would be the latest best practice to load Order using increment ID (instead of Order ID) using OrderRepository

    **Not a duplicate** - the linked answer is about retrieving the order by order id, not by increment id

    ekk! Is there a way to undo the mark as duplicate? I meant to mark the answer below as the answer to the solution.

    Too late, now that it is already closed. But I nominated the question to be reopened.

  • Jacques

    Jacques Correct answer

    5 years ago

    Magento 2 uses Service Contracts for retrieving and saving objects. In Magento this layer is formed by Repositories, which are managers with get() and save() methods. This keeps user code away from Model calls. Don't call model methods (like load() or save() or loadByIncrementId()) directly, they are being deprecated as custom code should use the Service Contracts. Also, don't use the API from within Magento like Khoa is suggesting, it does not make sense. The API is for connecting Magento with other systems.

    Inject OrderRepository and SearchCriteriaBuilder in your constructor:

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

    And in your function:

    $searchCriteria = $this->searchCriteriaBuilder
        ->addFilter('increment_id', '000000001', 'eq')->create();
    $orderList = $this->orderRepository->getList($searchCriteria)->getItems();
    
    // $orderList is now an array of Orders with this incrementId, which is just one order obviously
    
    /** @var \Magento\Sales\Model\Order $order */
    $order = reset($orderList);
    // Your logic here
    $order = $this->orderRepository->save($order);
    

    Official Magento PHP Developer guide on magento.com

    Code by Mulderea on github

    I am getting error like : Cannot use object of type Magento\Sales\Model\ResourceModel\Order\Collection as array at $order = $orderList[0];

    That's odd, I got an array. Which version are you on? But with a collection you can just replace `$order = $orderList[0]` with `$order = $orderList->getFirstItem()` to get the first (and only) item from the collection.

    I am in version 2.1.3. and I need to load order object for load payment object .. I have done `$order = $orderList->getFirstItem(); $payment = $order->getPayment();` But getting nothing in `$payment` Please check my problem here http://magento.stackexchange.com/questions/158935/magento-2-unable-to-set-payment-transaction-id-after-successful-payment-process

    I had a bug with the accepted answer. This solved it. Thanks for the good solution

    In Magento 2.3.x I need to get the order this way: `$order = $orderList['000000001'];`

    @ph.dev `$order = end($orderList)` would be better since `'000000001'` here is order ID which you probably don't know.

    @KrzysztofWołowski Upvote and you are right. `$order = reset($orderList)` could be used too, and maybe more semantic.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM