The Magento 2 developers know the crazy client requirements
If you have been there too, you might know what I’m talking about. But for newbies who are yet to face them, this post might be helpful!
It gives you the programmatic solution to get order information by order ID in Magento 2.
You can use this solution in different scenarios like restrict order based on the delivery address, offer a specific discount based on the order amount, offer a free product when a customer buys a particular item, etc.
With the below solution, you can get the order details like order items, order amount, billing and shipping addresses, order payment method, billing details, quantity, and the customer name.
Method to Get Order Information By Order Id in Magento 2:
$orderId = 123; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId); // Get Order Information $order->getEntityId(); $order->getIncrementId(); $order->getState(); $order->getStatus(); $order->getStoreId(); $order->getGrandTotal(); $order->getSubtotal(); $order->getTotalQtyOrdered(); $order->getOrderCurrencyCode(); // get customer details $custLastName = $orders->getCustomerLastname(); $custFirsrName = $orders->getCustomerFirstname(); // get Billing details $billingaddress = $order->getBillingAddress(); $billingcity = $billingaddress->getCity(); $billingstreet = $billingaddress->getStreet(); $billingpostcode = $billingaddress->getPostcode(); $billingtelephone = $billingaddress->getTelephone(); $billingstate_code = $billingaddress->getRegionCode(); // get shipping details $shippingaddress = $order->getShippingAddress(); $shippingcity = $shippingaddress->getCity(); $shippingstreet = $shippingaddress->getStreet(); $shippingpostcode = $shippingaddress->getPostcode(); $shippingtelephone = $shippingaddress->getTelephone(); $shippingstate_code = $shippingaddress->getRegionCode(); $grandTotal = $order->getGrandTotal(); $subTotal = $order->getSubtotal(); // fetch specific payment information $amount = $order->getPayment()->getAmountPaid(); $paymentMethod = $order->getPayment()->getMethod(); $info = $order->getPayment()->getAdditionalInformation('method_title'); // Get Order Items $orderItems = $order->getAllItems(); foreach ($orderItems as $item) { $item->getItemId(); $item->getOrderId(); $item->getStoreId(); $item->getProductId(); print_r($item->getProductOptions()); $item->getSku(); $item->getName(); $item->getQtyOrdered(); $item->getPrice(); }
That’s it.
You may want to bookmark this post for future reference!
I’d be very grateful if you help share this helpful post on social media to fellow developers!
Thanks!