You can get the Shipment collection data for the specific shipment by shipment id in Magento 2.
Using Magento\Sales\Api\ShipmentRepositoryInterface interface, you need to use get() method to fetch shipment data by its id.
Pass shipment id as get method, get($shipmentId) arguments.
When you check the sales_shipment table, You can get the result of all the field value with a specific id.
To get shipment records, you must have shipment id and you can get the result using below code snippet.
<?php namespace Path\To\Class; use Exception; use Psr\Log\LoggerInterface; use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Api\ShipmentRepositoryInterface; class ShipmentData { /** * @var ShipmentRepositoryInterface */ private $shipmentRepository; /** * @var LoggerInterface */ private $logger; public function __construct( ShipmentRepositoryInterface $shipmentRepository, LoggerInterface $logger ) { $this->shipmentRepository = $shipmentRepository; $this->logger = $logger; } /** * Get Shipment data by Shipment Id * * @param $id * * @return ShipmentInterface|null */ public function getShipmentById($id) { try { $shipment = $this->shipmentRepository->get($id); } catch (Exception $exception) { $this->logger->critical($exception->getMessage()); $shipment = null; } return $shipment; } }
Using above code snippet you can get the shipment data.
$id = 1; // shipment id $shipmment = $this->getShipmentById($id); var_dump($shipmment->debug());
Output:
Array ( [entity_id] => 1 [store_id] => 1 [total_qty] => 5.0000 [order_id] => 1 [customer_id] => 1 [shipping_address_id] => 1 [billing_address_id] => 2 [increment_id] => 000000001 [created_at] => 2019-08-17 12:19:20 [updated_at] => 2019-08-17 12:19:20 [packages] => Array ( ) )