且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法在 Magento 1.6.2 中更新产品的库存项目数量

更新时间:2023-11-26 23:14:52

您所缺少的只是保存 $stockItem.您不需要创建新的 stock_item,也不需要保存产品.

All you were missing is to save the $stockItem. You shouldn't need to create a new stock_item nor should you have to save the product.

if (!($stockItem = $product->getStockItem())) {
    $stockItem = Mage::getModel('cataloginventory/stock_item');
    $stockItem->assignProduct($product)
              ->setData('stock_id', 1)
              ->setData('store_id', 1);
}
$stockItem->setData('qty', $stockQty)
          ->setData('is_in_stock', $stockQty > 0 ? 1 : 0)
          ->setData('manage_stock', 1)
          ->setData('use_config_manage_stock', 0)
          ->save();