Magento:在数量更新时自动更改inventory_stock_availability

时间:2014-12-04 12:57:52

标签: php magento magento-1.9

在我们的Magento商店,我们使用扩展程序"条形码运输"(http://www.magentocommerce.com/magento-connect/barcode-shipping.html)或扩展程序中的自定义Productgrid更新我们的库存"增强型管理网格(+编者)" (http://www.magentocommerce.com/magento-connect/enhanced-admin-grids-editor.html)。

如果产品已售出且库存数量达到0,则属性inventory_stock_availability设置为" Out of Stock"。如果库存已更新,我们必须手动将属性inventory_stock_availability设置为" In Stock"。

有没有办法自动将库存可用性设置为"库存"当库存水平更新为大于0的值时?也许是一个自定义扩展来监听触发事件?

2 个答案:

答案 0 :(得分:1)

我通过使用@DWils答案和cronjob的组合找到了合适的方法。

在我的扩展配置中(app / code / local / My / 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>

我的观察员(app / code / local / My / UpdateStockAvailability / Model / Observer.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(app / etc / modules / My_UpdateStockAvailability.xml):

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <My_UpdateStockAvailability>
            <active>true</active>
            <codePool>local</codePool>
        </My_UpdateStockAvailability>
    </modules>
</config>

答案 1 :(得分:0)

您可以通过添加观察者来监视catalog_product_save_before并告诉它检查产品数量,以及是否&gt; 0(或触发缺货的最小值)

然后将标志inventory_stock_availability设置为1并返回。这将在产品保存发生之前更改标志。

请注意,如果根据商店的设置方式单独管理数量和库存标记,这可能会对商店的功能产生其他影响。例如,在我们拥有的一家商店,我们现在隐藏购买,如果它已经缺货,而且现有的任何数量都已经预留给了某个人,但尚未发货。

这篇文章详细介绍了如何创建这样的观察者 http://blog.chapagain.com.np/magento-event-observer-with-save-before-and-save-after/