设置magento产品的库存数量

时间:2014-07-24 19:05:59

标签: magento collections stock

我必须在我的magento商店中设置所有产品的数量,但我在这里的代码不起作用,是否有人知道我的代码有什么问题?

    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    $variable = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

foreach ($variable as $product) {

    $product->setManageStock(1)
    ->setUseConfigManageStock(1)
    ->setQty(1)
    ->setIsInStock(1)
    ->save();
}

1 个答案:

答案 0 :(得分:1)

这可能会有所帮助:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$variable = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');

foreach ($variable as $product) {
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getEntityId());

    $stockItem->setManageStock(1)
    ->setUseConfigManageStock(1)
    ->setQty(1)
    ->setIsInStock(1)
    ->save();
}