1

I Have Loaded A Simple Product Using Magento\Catalog\Model\ProductRepository And Changed Its Quantity To 1 And Stock Status (is_in_stock) To 0,

Using Function $productStock->setQuantityAndStockStatus(['qty' => $correctedStock, 'is_in_stock' => $isInStock]);

After This Am Not Able To Load The Product Programmatically , It Shows Following Error.

`1 exception(s): Exception #0 (Magento\Framework\Exception\NoSuchEntityException): Requested product doesn't exist

Using Magento CE 2.1.2

  • I think you miss some settings . 1st check is your product enable or not. – Debasis Chakraborty Nov 29 '16 at 11:19
  • its enabled, i have checked. – Rahul v Ramesh Nov 29 '16 at 11:21
  • after setting is_in_status = 0 , product will be disabled automatically ? – Rahul v Ramesh Nov 29 '16 at 11:21
  • set is_in_stock to 1 . I think 0 means out of stock – Debasis Chakraborty Nov 29 '16 at 11:25
  • Yah , I Want To make It out of stock , and set qty 1 because of some m2 bug – Rahul v Ramesh Nov 29 '16 at 16:45
0

Go to admin > Stores > configuration > Catalog > Inventory > Stock Options

Set Display Out of Stock Products to NO

0

You can create a Helper class (as given below ) to update product inventory as you want .

Assume you are using a custom extension Company_MyModule

<?php
namespace Company\MyModule\Helper;

use Magento\Framework\App\Filesystem\DirectoryList;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    /**
     * @var Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $_productRepository;

    /**
     * @var Magento\CatalogInventory\Api\StockStateInterface 
     */
    protected $_stockStateInterface;

    /**
     * @var Magento\CatalogInventory\Api\StockRegistryInterface 
     */
    protected $_stockRegistry;



    /**
    * @param Magento\Framework\App\Helper\Context $context
    * @param Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    * @param Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface,
    * @param Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
    */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\CatalogInventory\Api\StockStateInterface $stockStateInterface,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry    
    ) {
        $this->_productRepository = $productRepository;
        $this->_stockStateInterface = $stockStateInterface;
        $this->_stockRegistry = $stockRegistry;        
        parent::__construct($context);
    }

    /**
     * For Update stock of product
     * @param int $productId which stock you want to update
     * @param array $stockData your updated data
     * @return void 
    */
    public function updateProductStock($productId,$stockData) {
        $product = $this->_productRepository->getById($productId); //load product which you want to update stock
        $productSku = $product->getSku();
        $stockItem = $this->_stockRegistry->getStockItemBySku($productSku); // load stock of that product  by SKU                                       
        $stockItem->setIsInStock($stockData['is_in_stock']); //set updated data as your requirement
        $stockItem->setQty($stockData['qty']); //set updated quantity 
        $stockItem->setManageStock($stockData['manage_stock']);  
        $this->_stockRegistry->updateStockItemBySku($productSku,$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.