3

I need get the inventory source id based on the order item. I tried with order details but it not display. Anyone please help me.

6
  • You need to know source id/name from order_item/product_id ?
    – anonymous
    Commented Mar 29, 2019 at 6:32
  • Yes, I know the order_id and product_id Commented Apr 1, 2019 at 8:12
  • Did you get solution because its more than 2 week old !!?
    – anonymous
    Commented Apr 1, 2019 at 8:19
  • 1
    get the solution but i need any other alternative solution Commented Apr 1, 2019 at 11:12
  • @venkataprasad Did you get solution? Commented Jun 29, 2020 at 9:01

2 Answers 2

0

You can do like :

//Magento\InventoryApi\Api\GetSourceItemsBySkuInterface;
$sourceItems = $this->getSourceItemsBySku->execute('Your SKU');

foreach($sourceItems as $sourceItem){
    echo $sourceItem->getSourceCode();
}

Hope it's better then what you've used so far. ;)

3
  • 1
    it shows the all the sources of item having quantity. I need product quantity reduced source. Commented Apr 1, 2019 at 12:54
  • What do you mean by reduced source ? Please provide more details.
    – anonymous
    Commented Apr 1, 2019 at 13:02
  • The issue is you may have multiple sources for the same SKU. For example, one source has 2 dolls and another has 3 of them. A customer orders 2 dolls. They both come from the same source, but with your code we would get two sources. There should be a way to get which source was used for that specific order.
    – bfontaine
    Commented May 31, 2021 at 8:47
0

In Magento 2.3, you can retrieve the inventory source ID associated with an order by accessing the order details through the order object. Here's how you can do it: use Magento\Framework\App\Bootstrap; require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

// Load the order by order ID
$orderId = 123; // Replace with the actual order ID
$order = $objectManager->create('Magento\Sales\Model\Order')->load($orderId);

// Get the shipping assignment associated with the order
$shippingAssignment = $order->getShippingAssignment();

// Get the shipping address
$shippingAddress = $shippingAssignment->getShipping()->getAddress();

// Get the shipping method (e.g., carrier code)
$shippingMethod = $shippingAddress->getShippingMethod();

// Use the shipping method to determine the inventory source ID
// You may need to extract the source ID from the shipping method, depending on your setup
$inventorySourceId = null;
if (strpos($shippingMethod, 'source:') !== false) {
    // Extract the source ID from the shipping method
    list(, $inventorySourceId) = explode('source:', $shippingMethod);
}

if ($inventorySourceId !== null) {
    echo 'Inventory Source ID: ' . $inventorySourceId;
} else {
    echo 'Inventory Source ID not found for this order.';
}

In this code:

We load the order object using the order ID. We access the shipping assignment associated with the order. We get the shipping address from the shipping assignment. We retrieve the shipping method (which may contain the inventory source ID as part of its value). We extract the inventory source ID from the shipping method if it is present.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.