In this article, You will learn about how to UnAssign Inventory sources to bulk products and also you’ll learn how the inventory sources are unassigned with the source code. And which files we have to use to unassign the inventory sources programmatically.
Please follow these steps one by one:
Step-1: On the Admin Panel, go to the Catalog menu then select the products menu.(catalog > products). See the below Screenshot.

Step-2: Now you’ll see the products grid from here you can select one or more than one products to assign inventory sources.
And then check the Actions dropdown after selecting the products where you’ll find the UnAssign Inventory Sources menu click on the same. See the below Screenshot.

Step-3: Now after clicking on the same, You’ll see the page where you’ll find all the sources. see below screenshot.

Step-4: Now select the source which you want to unassign to all the selected products. And then click on the Unassign sources button which you’ll see on the upper right corner of the page click on the button unassign source. See the below screenshot.

In this way, you can unassign the inventory sources to the selected product .
Now to do the same programmatically go to file path vendor/magento/moduel-inventory-catalog/Model/ResourceModel/BulkSourceUnassign.php
public function execute(array $skus, array $sourceCodes): int
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM);
$connection->beginTransaction();
$count = (int) $connection->delete($tableName, [
SourceItemInterface::SOURCE_CODE . ' IN (?)' => $sourceCodes,
SourceItemInterface::SKU . ' IN (?)' => $skus,
]);
// Legacy stock update
if (in_array($this->defaultSourceProvider->getCode(), $sourceCodes)) {
$this->bulkZeroLegacyStockItem->execute($skus);
}
$connection->commit();
return $count;
}
here you will get the code with the function execute by which you can unassign inventory sources for bulk products just use this function on your code where ever you want.
In this function, you have to pass two params
1: SKU as an array.
2: Source code as an array.
Note: By bulk unassign inventory source product quantity will get changed with zero.

Be the first to comment.