Skip to content

Instantly share code, notes, and snippets.

@bka
Created May 10, 2017 09:22
Show Gist options
  • Save bka/d9315b76c6be0c2016611e23e13d52be to your computer and use it in GitHub Desktop.
Save bka/d9315b76c6be0c2016611e23e13d52be to your computer and use it in GitHub Desktop.
Magento2 Random Stock
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create("\Magento\Catalog\Model\ResourceModel\Product\Collection");
foreach($productCollection as $product) {
$productRepository = $objectManager->create("\Magento\Catalog\Api\ProductRepositoryInterface");
$q = rand(0, 1000);
$p = $productRepository->getById($product->getId());
$p->setStatus(($q > 0 ? 1 : 0));
$p->setStockData(array(
'qty' => $q,
'is_in_stock' => ($q > 0 ? 1 : 0)
));
$productRepository->save($product);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment