Magento Cronjob更新库存

时间:2014-01-28 06:41:00

标签: magento magento-1.7

经过长时间的搜索,我终于决定问:

我需要一个用于magento的Cronjob,每天更新一次“on stock”中的产品属性“ stock ”。

是否有可能捕获此属性并使用cronjob进行更改?

谢谢

2 个答案:

答案 0 :(得分:0)

我正在为此创建自定义扩展程序,请使用此功能 Cron每15分钟运行一次。

     Please create config.xml under app/code/local/Amit/CustomStockUpdate/etc/    
        <?xml version="1.0"?>
            <config>
              <modules>
                <Amit_CustomStockUpdate>
                  <version>0.1.0</version>
                </Amit_CustomStockUpdate>
              </modules>
                <global>
                    <models>
                        <customstockupdate>
                            <class>Amit_CustomStockUpdate_Model</class>
                        </customstockupdate>
                    </models>
                <helpers>
                  <customstockupdate>
                    <class>Amit_CustomStockUpdate_Helper</class>
                  </customstockupdate>
                </helpers>
              </global>
                    <crontab>
                        <jobs>
                            <customstockupdate_setting>
                            <schedule><cron_expr>*/15 * * * *</cron_expr></schedule>
                            <run><model>customstockupdate/observer::autostockupate</model></run>
                        </customstockupdate_setting>
                        </jobs>
                    </crontab>

            </config> 

      create Observer.php under app/code/local/Amit/CustomStockUpdate/Model/
    public function autostockupate(){
      // Disable the module itself

          $stock_item = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product_id);
if (!$stock_item->getId()) {
    $stock_item->setData('product_id', $product_id);
    $stock_item->setData('stock_id', 1); 
}

$stock_item->setData('is_in_stock', 1); // is 0 or 1
$stock_item->setData('manage_stock', 1); // should be 1 to make something out of stock

try {
    $stock_item->save();
} catch (Exception $e) {
    echo "{$e}";
}
    }

答案 1 :(得分:0)

以下是如何更新产品库存的示例。感谢

$mageproduct = Mage::getModel('catalog/product')->setStoreId($storeId)->loadByAttribute('sku', $Sku);
updateStockByProductId($mageproduct->getId(), $newQty);

function _updateStockByProductId($id, $qty) {
 $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
 if (!$stockItem->getId()) {
   $stockItem->setData('product_id', $id);
   $stockItem->setData('stock_id', 1); 
 }
  if ($stockItem->getQty() != $qty) {
   $stockItem->setData('qty', $qty);
   $stockItem->setData('is_in_stock', $qty ? 1 : 0);                
   $stockItem->save();               
  }        
  $stockItem->clearInstance();
}

您可以查看的其他链接 Magento更新产品库存 - &gt; http://www.ayasoftware.com/magento-update-product-inventory