2014-01-28 50 views

回答

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}"; 
} 
    } 
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的更新產品庫存 - >http://www.ayasoftware.com/magento-update-product-inventory