Get order data by increment id in magento 2
To get order first name, last name, order id, order total and other data using Magento\Sales\Api\Data\OrderInterfaceFactory interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php namespace CWM\Order\Helper; class OrderData extends \Magento\Framework\App\Helper\AbstractHelper { /* * \Magento\Sales\Api\Data\OrderInterfaceFactory */ protected $orderFactory; /** * @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Sales\Api\Data\OrderInterfaceFactory $orderFactory * @param $data */ public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Sales\Api\Data\OrderInterfaceFactory $orderFactory, array $data = [] ) { $this->orderFactory = $orderFactory; parent::__construct($context,$data); } /** * Retrieve order data from increment id * * @return object * @throws \Magento\Framework\Exception\LocalizedException */ public function getOrderByIcrementId($incrementId) { try { $order = $this->orderFactory->create()->loadByIncrementId($incrementId); } catch (Exception $e) { throw new \Magento\Framework\Exception\LocalizedException(__("Order does not exist.")); } return $order; } } |