无法在 Magento 1.6.2 中更新产品的库存项目数量 [英] Cannot update Stock Item Quantity for a Product in Magento 1.6.2

查看:31
本文介绍了无法在 Magento 1.6.2 中更新产品的库存项目数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从脚本中更新 Magento 中产品的库存数量.

I am trying to update the stock quantities of products in Magento from within a script.

我加载了产品,设置了库存数量,然后保存 - 但数量保持不变.

I load the product, set the stock quantity, and save - but the quantity remains unchanged.

// get stock data
$stockData = $product->getStockItem();
printf(PHP_EOL.'Stock: qty=%d, instock=%s, man_stock=%s, use_cfg_man_stock=%s'.PHP_EOL,
    $stockData->getData('qty'),
    $stockData->getData('is_in_stock'),
    $stockData->getData('manage_stock'),
    $stockData->getData('use_config_manage_stock')
);
// prints out qty=0, instock=, man_stock=, use_cfg_man_stock=


// $stockQty = 1
$product->stockItem->setData('qty', $stockQty);
$product->stockItem->setData('is_in_stock', $stockQty>0 ? 1 : 0);
$product->stockItem->setData('manage_stock', 1);
$product->stockItem->setData('use_config_manage_stock', 0);

$product->save();                           
$product->load();                           
$stockData = $product->getStockItem();
printf('New Stock: qty=%d, instock=%s, man_stock=%s, use_cfg_man_stock=%s'.PHP_EOL,
    $stockData->getData('qty'),
    $stockData->getData('is_in_stock'),
    $stockData->getData('manage_stock'),
    $stockData->getData('use_config_manage_stock')
);
// prints out qty=0, instock=, man_stock=, use_cfg_man_stock=

我哪里出错了?

推荐答案

您所缺少的只是保存 $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();

这篇关于无法在 Magento 1.6.2 中更新产品的库存项目数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆