Magento 2 Get Inventory Source Code from Shipment

Magento offers MSI (Multi Souce Invetnory) features that allow you to add multiple sources or locations for delivery. When shipping an order, you need to choose the source of inventory. If you want to find out which inventory source has been selected for an existing shipment, keep reading to learn how to do it!

How to get inventory source code of Magento shipment

You can use this code to get the shipment source code.
$shipment->getExtensionAttributes()->getSourceCode()

Here is the complete code for loading the shipment by ID and retrieving the source code in Magento 2.

<?php
namespace Dhairvi\Test\Model;

class ClassName
{
    /**
     * @var \Magento\Sales\Api\ShipmentRepositoryInterface
     */
    protected $_shipmentRepositoryInterface;

    /**
     * @param \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepositoryInterface
     */
    public function __construct(
        \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepositoryInterface
    ) {
        $this->_shipmentRepositoryInterface = $shipmentRepositoryInterface;
    }

    /**
     * Get shipment data using shipping ID.
     * 
     * @param $shipmentId
     * @return $sourceCode
     */
    public function getInventorySourceCode($shipmentId)
    {
        $sourceCode = null;
        try {
            $shipment = $this->_shipmentRepositoryInterface->get($shipmentId);
            $sourceCode = $shipment->getExtensionAttributes()->getSourceCode();
            
        } catch (Exception $exception)  {
            //error
        }

        return $sourceCode;
    }
}

This is how you can get the shipment source code in Magento 2.

Privacy Policy © 2024-2026 Dhairvi Solutions LLP. All Rights Reserved.