Magento的的cronjob更新股票(Magento Cronjob for updating

2019-10-29 17:47发布

经过长时间的搜寻后,我终于决定要问:

我需要的Magento一个cronjob,更新的产品属性“ 股票 ”的“股票”,每日一次。

有没有赶上这个属性,并用一个cronjob改变它的可能性?

谢谢

Answer 1:

我为创建一个自定义扩展,请使用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}";
}
    }


Answer 2:

下面是一个例子,如何更新库存产品。 谢谢

$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的更新产品库存- > http://www.ayasoftware.com/magento-update-product-inventory



文章来源: Magento Cronjob for updating stock