What would be the latest best practice to load Order using increment ID (instead of Order ID) using OrderRepository
Magento 2.1.1 Load Order by Increment ID Using OrderRepository
magento-2.1magento2moduleorders
Related Topic
- Magento2 Orders – How to Get Customer Name and Address Through Order Increment ID/Order ID
- Magento 2 – Get Order Increment ID from Invoice Increment ID
- Magento2 – Create POST API to Update Order Custom Fields Using Increment ID
- Magento2 – How to Set Order Increment ID While Creating Order Dynamically
- Magento2 – Load Order: OrderRepository vs OrderFactory
Best Answer
Magento 2 uses Service Contracts for retrieving and saving objects. In Magento this layer is formed by Repositories, which are managers with
get()
andsave()
methods. This keeps user code away from Model calls. Don't call model methods (likeload()
orsave()
orloadByIncrementId()
) 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:
And in your function:
Official Magento PHP Developer guide on magento.com
Code by Mulderea on github