
Hello Magento Folks,
In this blog post, we discussed how to get the shipping description from the order by order id in Magento 2.
To get the shipping description of the order using order objects, load the object using ‘order id’ from the order repository and then get the value by getShippingDescription() method as defined in the code snippet below in the ‘Helper’ class.
file: app/code/Webiators/CustomerAttribute/Helper/Data.php
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 41 42 43 44 45 46 47 48 49 |
<?php namespace Webiators\CustomerAttribute\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Exception; use Psr\Log\LoggerInterface; use Magento\Sales\Api\OrderRepositoryInterface; class Data extends AbstractHelper { /** * @var OrderRepositoryInterface */ private $orderRepository; /** * @var LoggerInterface */ private $logger; public function __construct( OrderRepositoryInterface $orderRepository, LoggerInterface $logger ) { $this->orderRepository = $orderRepository; $this->logger = $logger; } /** * @return string */ public function getShippingDescriptionByOrder() { $shippingDescription = ''; try { $orderId = '1'; // Dynamic order id $orderData = $this->orderRepository->get($orderId); $shippingDescription = $orderData->getShippingDescription(); } catch (Exception $exception) { $this->logger->error($exception->getMessage()); } return $shippingDescription; } } } |
Call the method getShippingDescriptionByOrder() at a proper place and pass order id in the method with your custom order id of an order.
If you have used Free shipping method for an Order,
Output –
Free shipping – Free
Any doubts? Feel free to share them in the Comments section below.
Thank you.