The question:
I want to update product qty and change Stock Status as in stock by programmatically by product id. I dont want to insert new product, but I want only update existing product by its id. Any one know how to do it ? I tried following code but not getting result.
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$storeManager = $obj->get('MagentoStoreModelStoreManagerInterface');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
//$productMetadata = $objectManager->get('MagentoFrameworkAppProductMetadataInterface');
//echo 'VERSION ==> '.$productMetadata->getVersion();
$product_id = 1;
$product = $objectManager->get('MagentoCatalogModelProduct')->load($product_id);
$productRepository = $objectManager->create('MagentoCatalogApiProductRepositoryInterface');
$StockState = $objectManager->get('MagentoCatalogInventoryApiStockStateInterface');
//$product = $objectManager->get('MagentoCatalogModelProduct')->load($product_id);
$product->setQty(100);
$product->save();
Edit : i want to run in a root directory and simple one php file. I want to run as
localhost/m229/updateproduct.php
The Solutions:
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Method 1
You can use below sample code
$product->setStockData(['qty' => $yourQty, 'is_in_stock' => 1]);
$product->setQuantityAndStockStatus(['qty' => $yourQty, 'is_in_stock' => 1]);
$product->save();
Method 2
You can use below code snippet in your script to update the stock data and status of the product.
$stockData = [
'use_config_manage_stock' => 0, // checkbox for 'Use config settings'
'manage_stock' => 1, // manage stock
'min_sale_qty' => 1, // Shopping Cart Minimum Qty Allowed
'max_sale_qty' => 2, // Shopping Cart Maximum Qty Allowed
'is_in_stock' => 1, // Stock Availability of product
'qty' => 100 // qty of product
];
$product->setStockData($stockData);
$product->setQuantityAndStockStatus($stockData);
$product->save();
Also, you can use the below code in your helper or block class in your custom module to update product stock status.
protected $_product;
/**
* @var MagentoCatalogInventoryApiStockStateInterface
*/
protected $_stockStateInterface;
/**
* @var MagentoCatalogInventoryApiStockRegistryInterface
*/
protected $_stockRegistry;
/**
* @param MagentoFrameworkAppHelperContext $context
* @param MagentoCatalogModelProduct $product
* @param MagentoCatalogInventoryApiStockStateInterface $stockStateInterface,
* @param MagentoCatalogInventoryApiStockRegistryInterface $stockRegistry
*/
public function __construct(
MagentoFrameworkAppHelperContext $context,
MagentoCatalogModelProduct $product,
MagentoCatalogInventoryApiStockStateInterface $stockStateInterface,
MagentoCatalogInventoryApiStockRegistryInterface $stockRegistry,
) {
$this->_product = $product;
$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->_product->load($productId); //load product which you want to update stock
$stockItem=$this->_stockRegistry->getStockItem($item['product_id']); // load stock of that product
$stockItem->setData('is_in_stock',$stockData['is_in_stock']); //set updated data as your requirement
$stockItem->setData('qty',$stockData['qty']); //set updated quantity
$stockItem->setData('manage_stock',$stockData['manage_stock']);
$stockItem->setData('use_config_notify_stock_qty',1);
$stockItem->save(); //save stock of item
$product->save(); // also save product
}
Hope it helps!!!
Method 3
Note: use of Object manager is not recommended, but since you are using custom script, I will also give you answer using object manager
Try this code:
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productRepository = $objectManager->create('MagentoCatalogModelProductRepository');
$stockRegistry = $objectManager->create('MagentoCatalogInventoryApiStockRegistryInterface');
$id = 1;
$product = $productRepository->getById($id);
$stockItem = $stockRegistry->getStockItemBySku($product->getSku());
$stockItem->setQty(100);
$stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
Hope this will work for you
Method 4
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$storeManager = $obj->get('MagentoStoreModelStoreManagerInterface');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
//$productMetadata = $objectManager->get('MagentoFrameworkAppProductMetadataInterface');
//echo 'VERSION ==> '.$productMetadata->getVersion();
$product_id = 1;
$product = $objectManager->get('MagentoCatalogModelProduct')->load($product_id);
$productRepository = $objectManager->create('MagentoCatalogApiProductRepositoryInterface');
$StockState = $objectManager->get('MagentoCatalogInventoryApiStockStateInterface');
//$product = $objectManager->get('MagentoCatalogModelProduct')->load($product_id);
//$product->setQty(100);
$yourQty = 100;
$product->setStockData(['qty' => $yourQty, 'is_in_stock' => 1]);
$product->setQuantityAndStockStatus(['qty' => $yourQty, 'is_in_stock' => 1]);
$product->save();
This is works for me. i copied some code from Narayan’s answer.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0