Back to Top

Magento 2 Assign Inventory Source for bulk product And Assign Inventory programmatically

Updated 13 July 2022

In this article, You will learn about how to Assign Inventory sources to bulk products and also you’ll learn how the inventory sources are assigned with the source code. And which files we have to use to assign 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_247

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 Assign Inventory Sources menu click on the same. See the below Screenshot.

Start your headless eCommerce
now.
Find out More
Selection_246-2

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

Selection_248

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

Selection_249

Step-5: After that, you’ll be redirected to the product grid where you’ll find the Quantity per source coloumn in the product grid. see below screenshot

Selection_250

In this way, you can bulk assign the inventory sources to the selected product .

Now to do the same programmatically go to file path vendor/magento/moduel-inventory-catalog/Model/ResourceModel/BulkSourceAssign.php

public function execute(array $skus, array $sourceCodes): int
{
    $types = $this->getProductTypesBySkus->execute($skus);
    $connection = $this->resourceConnection->getConnection();
    $tableName = $this->resourceConnection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM);
    $count = 0;
    foreach ($types as $sku => $type) {
        if ($this->isSourceItemManagementAllowedForProductType->execute($type)) {
            foreach ($sourceCodes as $sourceCode) {
            try {
                $connection->insert($tableName, [
                SourceItemInterface::SOURCE_CODE => $sourceCode,
                SourceItemInterface::SKU => $sku,
                SourceItemInterface::QUANTITY => 0,
                SourceItemInterface::STATUS => SourceItemInterface::STATUS_OUT_OF_STOCK,
                ]);
                $count++;
            } catch (DuplicateException $e) {
                // Skip if source assignment is duplicated
                continue;
            }
        }
    }
    }
    return $count;
}

here you will get the code with the function execute by which you can assign 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 assign inventory source product quantity will get changed with zero as by default bulk source assign Quantity is 0 from the core file.


To update the source quantity you can follow the next blog: https://webkul.com/blog/magento-2-update-assigned-source-inventory-from-admin-end-and-programmatically/

. . .

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