我正在尝试编写一个脚本,使用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'没有变化,我无法弄清楚我的代码中有什么问题。
提前感谢您的帮助和耐心
答案 0 :(得分:0)
您使用过setStockData,请尝试以下操作:
$stockItem->setData('is_in_stock', (int) $inStock["NOTAVAILABLE"]);
或
$stockItem->setIsInStock((int)$inStock["NOTAVAILABLE"]);