$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($id);
$productStockData = $this->stockRegistry->getStockItem($product->getId());
$productStockData->setData('is_in_stock', 0);
$productStockData->setData('qty', 0);
$productStockData->setData('manage_stock', 1);
$productStockData->save();
echo "<pre>";
print_r($productStockData->getData());
exit;
$product->save();
此代码在数组打印中使用qty提供正确的结果,但未在数据库中反映..
当打印产品数据时,它会返回qty
0(零)
答案 0 :(得分:3)
您可以使用以下代码设置库存数据。参考Click Here
$productStockData->setStockData(array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => 1, //Stock Availability
'qty' => 1 //qty
)
);