Magento Cronjob for updating stock

2019-08-31 07:06发布

问题:

after a long search I've finally decided to ask:

I need a Cronjob for magento that updates the product attribute "stock" in "on stock" once a day.

Is there a possibility to catch this attribute and change it with a cronjob?

Thank you

回答1:

I am creating a custom extension for that ,please use this Cron is run every 15 min.

     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}";
}
    }


回答2:

Here is an example how to update the stock for a product. Thanks

$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();
}

Other Link that you can check Magento update product inventory -> http://www.ayasoftware.com/magento-update-product-inventory