In this post I am going to explain how to get shipment details by shipment id in Magento 2.
Using below code snippet you can get shipment details by passing shipment id.
1. Using Dependency Injection
Add below code snippet in Block class.
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 | protected $_shipmentRepositoryInterface; protected $_logger; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepositoryInterface, \Psr\Log\LoggerInterface $logger, array $data = [] ) { $this->_shipmentRepositoryInterface = $shipmentRepositoryInterface; $this->_logger = $logger; parent::__construct($context, $data); } public function getShipmentDetails($shipmentId) { $shipmentData = array(); if($shipmentId > ) { try { $shipmentData = $this->_shipmentRepositoryInterface->get($shipmentId); } catch (Exception $exception) { $this->logger->critical($exception->getMessage()); $shipmentData = null; } } return $shipmentData; } |
Add below code snippet in template file.
1 2 3 4 5 6 7 8 | // Shipment Id $shipmentId = 1; $shipmentData = $block->getShipmentDetails($shipmentId); echo "<pre>"; print_r($shipmentData->getData()); echo "</pre>"; |
2. Using Object Manager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Shipment Id $shipmentId = 1; $shipmentRepositoryInterface = $objectManager->get('Magento\Sales\Api\ShipmentRepositoryInterface'); $logger = $objectManager->get('Psr\Log\LoggerInterface'); try { $shipmentData = $shipmentRepositoryInterface->get($shipmentId); } catch (Exception $exception) { $logger->critical($exception->getMessage()); $shipmentData = null; } echo "<pre>"; print_r($shipmentData->getData()); echo "</pre>"; |
Leave a Reply