2016-11-02 54 views
1
$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(); 

此代碼給出了數量在陣列打印但在數據庫不反映正確的結果..有沒有什麼方法可以使用前端控制器以編程方式更新數量?

當RY打印產品數據然後將其返回qty 0(零)

Result

回答

2

您可以使用以下代碼設置庫存數據。參考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 
     ) 
    ); 
+0

謝謝,它的工作原理 –

相關問題