【问题标题】:Setting the 'is_in_stock' for product in magento在 magento 中为产品设置“is_in_stock”
【发布时间】:2016-01-07 08:09:13
【问题描述】:

我正在尝试编写一个脚本来使用 php/mage 为 Magento 中的产品 SKU 设置更改为“is_in_stock”。

这是我目前在 Magento 1.9.2.1 中所拥有的

require_once $_SERVER['DOCUMENT_ROOT'].'/app/Mage.php';
$app = Mage::app();
set_time_limit(0);

$inStock = array('NOTAVAILABLE'=>'0','AVAILABLE'=>'1');
$updated=0;

$SKU = '801647';
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);

if ($product) {
    //Product found, so we need to update it in Magento.

    $productId = $product->getId();
    $stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
    $stockItemId = $stockItem->getId();

    echo "is_in_stock before: ",$stockItem->getIsInStock()," <br />";


    if((int) $stockItem->getIsInStock() !== (int) $inStock["NOTAVAILABLE"]){
        try {

            $stockItem->setStockData('is_in_stock', (int) $inStock["NOTAVAILABLE"]);

            $stockItem->save();
            $product->save();
            $updated++;

            echo "SKU: '",$SKU,"' <br /> Name: '",(string) $product->name,"', <br />is_in_stock after: ",$stockItem->getIsInStock()," <br />";
        }
    }
}

这是输出:

 is_in_stock before: 1 

 Product: 
 SKU: '801647' 
 Name: 'BAMBOLA PIPEDREAM EXTREME DOLLZ VARSITY VICKY LIFE-SIZE LOVE DOLL', 

 is_in_stock after: 1               

如您所见,“is_in_stock”没有任何变化,我无法弄清楚我的代码有什么问题。

提前感谢您的帮助和耐心

【问题讨论】:

  • 你想实现什么你想把'is_in_stock'从1变成0?

标签: php magento


【解决方案1】:

您已使用 setStockData,请尝试以下操作:

$stockItem->setData('is_in_stock', (int) $inStock["NOTAVAILABLE"]);

$stockItem->setIsInStock((int)$inStock["NOTAVAILABLE"]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多