0

I tried to import a product using CSV with is_in_stock value to '0' and after the import, the stock status still shows "In stock"

CSV looks like this :

sku,is_in_stock

DC3508BR,0

What am I doing wrong?

thanks

0

In order to update stock in Magento 2 using csv, for most scenarios, you will only need to include 'sku' and 'qty' columns.

However, I am assuming for the products in question you already have stock management turned on either globally or per product and you are not using 'backorders'. For more details on advanced stock management see the Magento documentation: http://docs.magento.com/m2/ce/user_guide/catalog/inventory-product-stock-options.html

is_in_stock (which maps to 'Stock Availability') is not required either as in most scenarios (based on assumptions above) Magento will auto set this based on whether you provide a stock qty of above 0 or not (i.e. qty of 0 or less will set Out of Stock, 1 or above will set 'In Stock')

So, in your case you just need to define 'sku' and 'qty'. Stock Availability will be auto selected based on QTY.

0

Here you have a working code sample, we use it in a custom console command. Here we use \Magento\Catalog\Model\ProductRepository as $productRepository & \Magento\CatalogInventory\Api\StockRegistryInterface as $stockRegistry

In this code, we set $isInStock value dynamically, based in $qty value

try {
    $product = $productRepository->get($sku);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e){
    $output->writeln('<info>Could not load!<info>');
    continue;
}

$stockItem = $stockRegistry->getStockItem(
    $product->getId(),
    $product->getStore()->getWebsiteId()
);

$stockItem->setData('qty',$qty);
$stockItem->setData('is_in_stock',$isInStock);
$stockRegistry->updateStockItemBySku($sku, $stockItem);

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.