我找到了一個合適的方法,通過使用@DWils的組合接聽和的cronjob。
在我的分機配置(應用程序/代碼/地方/我的/ UpdateStockAvailability的/ etc/config.xml文件):
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<My_UpdateStockAvailability>
<version>1.0.0</version>
</My_UpdateStockAvailability>
</modules>
<global>
<models>
<my_updatestockavailability>
<class>My_UpdateStockAvailability_Model</class>
</my_updatestockavailability>
</models>
<events>
<catalog_product_save_after>
<observers>
<my_updatestockavailability>
<class>my_updatestockavailability/observer</class>
<method>updateStockAvailability</method>
<type>singleton</type>
</my_updatestockavailability>
</observers>
</catalog_product_save_after>
</events>
</global>
<crontab>
<jobs>
<updatestockavailabilitycronjob>
<schedule>
<cron_expr>*/15 7-19 * * *</cron_expr>
</schedule>
<run>
<model>my_updatestockavailability/observer::updateCronAction</model>
</run>
</updatestockavailabilitycronjob>
</jobs>
</crontab>
</config>
我的觀察者(應用程序/代碼/地方/我的/ UpdateStockAvailability /型號/觀察員。 PHP):
<?php
class My_UpdateStockAvailability_Model_observer {
public function updateStockAvailability(Varien_Event_Observer $observer) {
$product = $observer->getProduct();
$stockData = $product->getStockData();
if ($product && $stockData['qty']) {
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getEntityId()); // Load the stock for this product
$stock->setData('is_in_stock', 1); // Set the Product to InStock
$stock->save(); // Save
}
}
public function updateCronAction() {
$collection = Mage::getResourceModel('cataloginventory/stock_item_collection');
$outQty = Mage::getStoreConfig('cataloginventory/item/options_min_qty');
$collection->addFieldToFilter('qty', array('gt' => $outQty));
$collection->addFieldToFilter('is_in_stock', 0);
foreach ($collection as $item) {
$item->setData('is_in_stock', 1);
}
$collection->save();
$bundleCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToSelect('type')
->addAttributeToFilter('type_id', 'bundle')
->joinField('is_in_stock', 'cataloginventory/stock_item', 'is_in_stock', 'product_id=entity_id', '{{table}}.stock_id=1', 'left')
->addAttributeToFilter('is_in_stock', array('eq' => 0));
foreach ($bundleCollection as $bundle) {
$selectionCollection = $bundle->getTypeInstance(true)->getSelectionsCollection($bundle->getTypeInstance(true)->getOptionsIds($bundle), $bundle);
$allInStock = true;
foreach ($selectionCollection as $option) {
$stockItem = $option->getStockItem();
if ($stockItem->getQty() <= 0 || $stockItem->getIsInStock() <= $outQty) {
$allInStock = false;
}
}
if ($allInStock) {
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($bundle);
$stockItem->setData('is_in_stock', 1);
$stockItem->save();
}
}
}
}
最後模塊的XML(表觀的/ etc /模塊/ My_UpdateStockAvailability.xml):
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<My_UpdateStockAvailability>
<active>true</active>
<codePool>local</codePool>
</My_UpdateStockAvailability>
</modules>
</config>
該方法工作,如果曲antity在magento後端更新。我們正在使用通過API更新數量的社區擴展BarcodeShipping。我使用cronjob找到了一個合適的解決方案(詳見我的回答)。 – freakimkaefig 2015-07-29 08:30:47