3

I'm using MSI for inventory management.

I've imports products and by default Magento assigned default stock to every products.

So I need to reassign products to my custom created stock that is under Stores->Configurations->stocks

I've created new stock from above and assigned my sources to it.

Than after I've imported sources using default import/export functions.

After than product updated as per data provided.

But issue I'm facing is after I import sources then previously assigned stock and sources information is still there and displays at product grid as well as in product form.

Can anybody have solution to this ? In short I want to remove old stock which is currently out-of-stock ?

Any hack/workaround will also work. Thanks

1

You could use my below script to remove Stock sources from all products. It loops through all products, and removes the stock source you enter in the foreach loop.

file: pub/RemoveStock.php

<?php

if(php_sapi_name() !== 'cli') {
echo 'You shall not pass!';
}

error_reporting(E_ALL);
ini_set('display_errors', 1);

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$sourceItemInterfaceFactory = $objectManager->get('Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory');
$sourceItemsDeleteInterface = $objectManager->get('Magento\InventoryApi\Api\SourceItemsDeleteInterface');



$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productCollection->create()->addAttributeToSelect('*')->load();

foreach ($collection as $product){
$sourceItem = $sourceItemInterfaceFactory->create();

$sourceItem->setSourceCode('<set stock id here!>');
$sourceItem->setSku($product->getSku());

$sourceItemsDeleteInterface->execute([$sourceItem]);
}

You can run the script by with this command: php RemoveStock.php

Hope this helps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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