Created
          May 10, 2017 09:22 
        
      - 
      
- 
        Save bka/d9315b76c6be0c2016611e23e13d52be to your computer and use it in GitHub Desktop. 
    Magento2 Random Stock
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | $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