You can get the Shipment collection for the specific order by order increment id in Magento 2.
Using Magento\Sales\Api\ShipmentRepositoryInterface interface, you need to use getList() function to fetch no. of Shipment by sales order increment id.
When you check the sales_shipment table, one field order_id is available in sales_shipment table so you first fetch the order id from the order increment id.
To get Shipment records, you first fetch the order id by order increment id and pass order id as SearchCriteriaBuilder addFilter() method.
Pass search criteria object to getList() method of ShipmentRepositoryInterface to fetch Shipment details by order increment id.
<?php namespace Path\To\Class; use Exception; use Psr\Log\LoggerInterface; use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Sales\Api\ShipmentRepositoryInterface; class ShipmentByOrderIncrementId { /** * @var ShipmentRepositoryInterface */ private $shipmentRepository; /** * @var SearchCriteriaBuilder */ protected $searchCriteriaBuilder; /** * @var OrderRepositoryInterface */ private $orderRepository; /** * @var LoggerInterface */ private $logger; public function __construct( ShipmentRepositoryInterface $shipmentRepository, SearchCriteriaBuilder $searchCriteriaBuilder, OrderRepositoryInterface $orderRepository, LoggerInterface $logger ) { $this->shipmentRepository = $shipmentRepository; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->orderRepository = $orderRepository; $this->logger = $logger; } /** * Get Shipment data by Order Id * * @param $incrementId * @return ShipmentInterface[]|null */ public function getShipmentDataByOrderId($incrementId) { // first fetch order entity id by increment id $orderId = $this->getOrderIdByIncrementId($incrementId); if ($orderId) { $searchCriteria = $this->searchCriteriaBuilder ->addFilter('order_id', $orderId)->create(); try { $shipments = $this->shipmentRepository->getList($searchCriteria); $shipmentRecords = $shipments->getItems(); } catch (Exception $exception) { $this->logger->critical($exception->getMessage()); $shipmentRecords = null; } } return $shipmentRecords; } /** * Get Order entity id by Order Increment Id * @param $incrementId * @return int|null */ public function getOrderIdByIncrementId($incrementId) { $searchCriteria = $this->searchCriteriaBuilder ->addFilter('increment_id', $incrementId)->create(); $order = $this->orderRepository->getList($searchCriteria); $orderId = null; if ($order->getTotalCount()) { foreach ($order->getItems() as $orderData) { $orderId = (int)$orderData->getId(); } } return $orderId; } }
Using the above way you got the output as Shipment data by order increment id.
You need to pass increment_id of order to fetch shipment of orders.
$orderIncrementId = 000000001; // order id $shipments = $block->getShipmentDataByOrderId($orderIncrementId); foreach ($shipments as $shipment) { # code... echo "<pre>";print_r($shipment->getData()); }
Output:
Array ( [entity_id] => 1 [store_id] => 1 [total_weight] => [total_qty] => 5.0000 [email_sent] => [send_email] => [order_id] => 1 [customer_id] => 1 [shipping_address_id] => 1 [billing_address_id] => 2 [shipment_status] => [increment_id] => 000000001 [created_at] => 2019-08-17 12:19:20 [updated_at] => 2019-08-17 12:19:20 [packages] => Array ( ) [shipping_label] => [customer_note] => [customer_note_notify] => [extension_attributes] => Magento\Sales\Api\Data\ShipmentExtension Object ( [_data:protected] => Array ( [source_code] => default ) ) )
The result will be no. of shipments generated for an Order.