I am trying to update the stock quantities of products in Magento from within a script.
我正在尝试从一个脚本中更新Magento的库存产品。
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=
Where am I going wrong?
我哪里做错了?
4 个解决方案
#1
16
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.
你所缺少的只是保存stockItem。您不需要创建一个新的stock_item,也不需要保存产品。
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();
#2
2
I've solved it myself:
我自己已经解决了:
// 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
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($product);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('stock_id', 1);
$stockItem->setData('store_id', 1);
$stockItem->setData('manage_stock', 0);
$stockItem->setData('use_config_manage_stock', 0);
$stockItem->setData('min_sale_qty', 0);
$stockItem->setData('use_config_min_sale_qty', 0);
$stockItem->setData('max_sale_qty', 1000);
$stockItem->setData('use_config_max_sale_qty', 0);
$stockItem->setData('qty', $stockQty);
$stockItem->save();
$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=1, instock=1, man_stock=0, use_cfg_man_stock=0
By creating a new StockItem, and assigning that to the product. Hope this helps someone else.
通过创建一个新的StockItem,并将其分配给产品。希望这能帮助别人。
#3
2
To display stock quantity for the product -
显示产品的库存数量-
<?php echo $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product); ?>
And if you want to display individual data use this -
如果你想显示单独的数据使用这个-
<?php echo $stock->getQty(); ?> // Will display stock quantity
<?php echo $stock->getMinQty(); ?> // Will display minimum quantity
<?php echo $stock->getMinSaleQty(); ?> //will display minimum salable quantity
#4
0
In my case I got the same problem with products that I've imported programmatically. I just realized that I forgot to add stock_data in the array...
在我的例子中,我遇到了与我以编程方式导入的产品相同的问题。我刚意识到我忘记在数组中添加stock_data了…
'name' => $productName,
'stock_data' => array(
'use_config_manage_stock' => 0,
'manage_stock' => 0,
'qty' => 0,
'stock_id' => 1,
'min_qty' => 0,
)
Without stock data information, Magento generates an exception(at \Mage_CatalogInventory_Model_Stock_Item) when adding product to cart.
在没有库存数据信息的情况下,在将产品添加到购物车时,Magento会生成一个异常(at \Mage_CatalogInventory_Model_Stock_Item)。
With this node, it works. =)
有了这个节点,它就可以工作了。=)
#1
16
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.
你所缺少的只是保存stockItem。您不需要创建一个新的stock_item,也不需要保存产品。
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();
#2
2
I've solved it myself:
我自己已经解决了:
// 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
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($product);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('stock_id', 1);
$stockItem->setData('store_id', 1);
$stockItem->setData('manage_stock', 0);
$stockItem->setData('use_config_manage_stock', 0);
$stockItem->setData('min_sale_qty', 0);
$stockItem->setData('use_config_min_sale_qty', 0);
$stockItem->setData('max_sale_qty', 1000);
$stockItem->setData('use_config_max_sale_qty', 0);
$stockItem->setData('qty', $stockQty);
$stockItem->save();
$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=1, instock=1, man_stock=0, use_cfg_man_stock=0
By creating a new StockItem, and assigning that to the product. Hope this helps someone else.
通过创建一个新的StockItem,并将其分配给产品。希望这能帮助别人。
#3
2
To display stock quantity for the product -
显示产品的库存数量-
<?php echo $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product); ?>
And if you want to display individual data use this -
如果你想显示单独的数据使用这个-
<?php echo $stock->getQty(); ?> // Will display stock quantity
<?php echo $stock->getMinQty(); ?> // Will display minimum quantity
<?php echo $stock->getMinSaleQty(); ?> //will display minimum salable quantity
#4
0
In my case I got the same problem with products that I've imported programmatically. I just realized that I forgot to add stock_data in the array...
在我的例子中,我遇到了与我以编程方式导入的产品相同的问题。我刚意识到我忘记在数组中添加stock_data了…
'name' => $productName,
'stock_data' => array(
'use_config_manage_stock' => 0,
'manage_stock' => 0,
'qty' => 0,
'stock_id' => 1,
'min_qty' => 0,
)
Without stock data information, Magento generates an exception(at \Mage_CatalogInventory_Model_Stock_Item) when adding product to cart.
在没有库存数据信息的情况下,在将产品添加到购物车时,Magento会生成一个异常(at \Mage_CatalogInventory_Model_Stock_Item)。
With this node, it works. =)
有了这个节点,它就可以工作了。=)