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.
-
You need to know source id/name from order_item/product_id ?– anonymousMar 29, 2019 at 6:32
-
Yes, I know the order_id and product_id– venkata prasadApr 1, 2019 at 8:12
-
Did you get solution because its more than 2 week old !!?– anonymousApr 1, 2019 at 8:19
-
1get the solution but i need any other alternative solution– venkata prasadApr 1, 2019 at 11:12
-
@venkataprasad Did you get solution?– Manisha VasaniJun 29, 2020 at 9:01
2 Answers
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. ;)
-
1it shows the all the sources of item having quantity. I need product quantity reduced source. Apr 1, 2019 at 12:54
-
-
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. May 31, 2021 at 8:47
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.