I am new to Magento 2 and customising Magneto 1 to Magento 2. I need some solutions regards to manage stocks. Let me give you a brief about it.

I am creating a method "updateInventoryStock($sku, $count = NULL)" in Magento 2. So here it the code in Magento 1.9 :

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($update->getId());
if ($stock->getManageStock()) {
    $stock->setIsInStock($count > $stock->getMinQty());

Now how can I replicate this in Magento 2? Help me out. Thanks


Stock Management has come a long way in Magento 2. There are modular methods to cater specific needs. I am not able to understand what you are trying to achieve. Can you please explain the purpose of the code and what will your code do?

Here are some methods with explanation by Magento - https://devdocs.magento.com/guides/v2.4/inventory/inventory-api-reference.html. Try to find the desired method there. If not, there are series of methods in Magento under the modules with namespace Inventory. You can always find more methods there.


I think you want to update the stock for a product programmaticaly, here is an example console command that I have done recently that you can use.

Also here is the link to how to create a console command

 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.

namespace W2\MassStockChange\Console\Command;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MassImport extends Command

    const NAME_ARGUMENT = "name";
    const NAME_OPTION = "option";

    private  $collectionFactory;
     * @var StockRegistryInterface
    private  $stockRegistry;
    private  $csvProcessor;
    private  $directoryList;
    private  $filesystem;

    public function __construct(
        CollectionFactory $collectionFactory,
        StockRegistryInterface $stockRegistry,
        \Magento\Framework\File\Csv $csvProcessor,
        \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
        \Magento\Framework\Filesystem $filesystem,
        string $name = null
        $this->collectionFactory = $collectionFactory;
        $this->stockRegistry = $stockRegistry;
        $this->csvProcessor = $csvProcessor;
        $this->directoryList = $directoryList;
        $this->filesystem = $filesystem;

     * {@inheritdoc}
    protected function execute(
        InputInterface $input,
        OutputInterface $output

        $csvFilename = $input->getArgument(self::NAME_ARGUMENT);
        $fileDirectoryPath = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
        $csvFile = $fileDirectoryPath.'/import/csv/';
        $csvFile .= $csvFilename;

        $data = $this->csvProcessor->getData($csvFile);

        foreach ($data as $row) {
            $output->writeln("<comment>.$row[0]. </>");
            $stockItem = $this->stockRegistry->getStockItemBySku($row[0]);
            $this->stockRegistry->updateStockItemBySku($row[0], $stockItem);

        $output->writeln("<info>Finished!!! </>");

     * {@inheritdoc}
    protected function configure()
        $this->setDescription("Stock Mass Modify");
            new InputArgument(self::NAME_ARGUMENT, InputArgument::OPTIONAL, "Name"),
            new InputOption(self::NAME_OPTION, "-a", InputOption::VALUE_NONE, "Option functionality")

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.