Back to Top

Magento 2 UnAssign Inventory Source for bulk product programmatically

Updated 17 October 2022

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.

Selection_341

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.

Selection_342

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

Start your headless eCommerce
now.
Find out More
Selection_343

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.

Selection_344

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.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home