freakimkaefig
freakimkaefig

Reputation: 419

Magento: Auto-change inventory_stock_availability on quantity update

In our Magento Shop we're updating our Stock with the extension "Barcode Shipping"(http://www.magentocommerce.com/magento-connect/barcode-shipping.html) or the custom Productgrid from the extension "Enhanced Admin Grids (+ Editor)" (http://www.magentocommerce.com/magento-connect/enhanced-admin-grids-editor.html).

If a product is sold and the stock quantity reaches 0, the attribute inventory_stock_availability is set to "Out of Stock". If the stock is updated, we always have to set the attribute inventory_stock_availability manually to "In Stock".

Is there a way to automatically set the stock availability to "In Stock" when the stock level updates to a value greater than 0? Maybe a custom extension listening to a triggered event?

Upvotes: 0

Views: 2078

Answers (2)

freakimkaefig
freakimkaefig

Reputation: 419

I found a suitable approach by using a combination of @DWils answer and cronjob.

In my extension config (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>

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

Finally the module 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>

Upvotes: 1

DWils
DWils

Reputation: 398

You could handle this by adding an observer to watch for catalog_product_save_before and tell it to check the product Qty, and if > 0 (or the minimum value for out of stock to be triggered)

Then set the flag inventory_stock_availability to 1 and return. That would change the flag before the product save happens.

Bear in mind that this may have other impacts on the store's functionality if the qty and in stock flags were managed separately, depending on how your shop was set up. For instance on one store we have, we hide the buy now if it's set to out of stock and any qty on hand is reserved for someone already, but has not yet shipped.

Here is one article detailing how one would create such an observer
http://blog.chapagain.com.np/magento-event-observer-with-save-before-and-save-after/

Upvotes: 0

Related Questions