0

I would like to do if any Child product has status "in-stock" parent status auto update "in-stock". else parent configurable product status update "out-stock". anyone can help me how can I do it with programming ? Thanks in advance.

0

You can using event observing like follows:

NS_ProChange is the module name.

Specify in app/code/local/NS/ProChange/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <NS_ProChange>
            <version>0.0.1</version>
        </NS_ProChange>
    </modules>
<global>
    <events>
        <catalog_product_save_after>
            <observers>
                <productchange>
                    <type>singleton</type>
                    <class>NS_ProChange_Model_Observer</class>
                    <method>detectProductChanges</method>
                </productchange>
            </observers>
        </catalog_product_save_after>
    </events>
</global>

Now in app/code/local/NS/ProChange/Model/Observer.php

class NS_ProChange_Model_Observer extends Varien_Event_Observer
{
    public function detectProductChanges($observer)
    {
        $product = $observer->getProduct();
        if( $product->getTypeId() != 'simple' ) {
            return false;
        }
        $parentId = Mage::getResourceSingleton('catalog/product_type_configurable')
            ->getParentIdsByChild( $product->getId() );

        if( $parentId[0] != '' ) {
            $inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getIsInStock();          
            $configProduct = Mage::getModel('catalog/product')->load( $parentId[0] );
            if( $configProduct->getTypeId() == 'configurable' ) {
                $configProduct->setData('in_stock',$inStock);
                $configProduct->save();
            }           
        }   
    }

Create module activation file - app/etc/modules/NS_ProChange.xml

<?xml version="1.0"?>
<config>
    <modules>
        <NS_ProChange>
            <active>true</active>
            <codePool>local</codePool>
        </NS_ProChange>
    </modules>
</config>
  • i am new in magento plz can You guide in detail ? – Grace Jan 14 '17 at 12:45
  • i created those file NS/ProChange/etc/config.xml , NS/ProChange/Model/Observer.php and i upload on my site root folder but its not working fine – Grace Jan 14 '17 at 12:47
  • Follow this tutorial - magenticians.com/create-magento-custom-module – sandip Jan 14 '17 at 14:05
  • @Grace now check edited answer – sandip Jan 14 '17 at 14:20
  • again thanks for response bro i did same that above You guide me but still not solve my issue on Stock Availability – Grace Jan 16 '17 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.