Magento – Save Updated Configurable Product Attribute to All Associated Products

adminhtmlconfigurable-productproduct-attribute

I'm trying to extend Magento to where, when I update a custom attribute for a configurable product, it saves it to the corresponding custom attribute for all of its associated products. I've got an observer that watches the catalog_product_save_after event but I'm not exactly sure where to proceed in order to get it to save it to the associated products. I've looked at a lot of solutions here and other sites that deal with programmatically creating a configurable product and it's assocaited products, but nothing that does what I need. Any help would be greatly appreciated.

EDIT: Here's the final code I ended up with:

public function copyAttribute($observer) {
        try {
            // get the event product
            $product = $observer->getEvent()->getProduct();

            // check to see if product is configurable
            if ($product->getData('type_id') === 'configurable') {

                // get associated products
                $associated = $product->getTypeInstance(true)->getUsedProducts(null, $product);

                // create attribute array
                $feedArray = array('feedprice1', 'feedprice2', 'feedprice3');

                // loop through associated products and save configurable products changed value
                foreach ($associated as $associate) {
                    foreach ($feedArray as $feed) {
                        $associate->setData($feed, $product->getData($feed));
                        $associate->getResource()->saveAttribute($associate, $feed);
                    }
                }
            }
        }

        // log any errors
        catch (Exception $e) {
            Mage::log(print_r($e->getMessage(), 1), 'null', 'product-attribute-update.log');
        }
    }

Best Answer

Create an event product save after only for configurable product save from admin.

    <adminhtml>
            <events>
            <catalog_product_save_after>
                <observers>
                    <stockalert>
                        <type>singleton</type>
                        <class>check/observer</class>
                        <method>autoupdatemy</method>
                    </stockalert>
                </observers>
            </catalog_product_save_after>
        </events>
</adminhtml>

Then using observer update field of child products:i have update here meta description of child products using parent products meta description Code is Here

public function autoupdatemy($observer)
{
    try{

    if($observer->getEvent()->getProduct()->getData('type_id')=='configurable'){
        $ConfiProduct=$observer->getEvent()->getProduct();
        $allProducts = $ConfiProduct->getTypeInstance(true)
            ->getUsedProducts(null, $ConfiProduct);
        foreach ($allProducts as $product) {
            $product->setData('meta_title',$ConfiProduct->getData('meta_title'));
            $product->getResource()->saveAttribute($product, 'meta_title');
            Mage::log('myaddr_new_code after-testingnew-'.$product->getData('name').'--'.$product->getId(), null, 'mage32173.log');
        }
    }
    }catch(Excpetion $e){
        Mage::log(print_r($e->getMessage(),1),'null','mage32173.log');
    }

    return;

}

Let me know,if you have any issue

As your requested Just add below code

EDIT:

$product->setData('attributecode_1',$ConfiProduct->getData('attributecode_1'));
$product->getResource()->saveAttribute($product, 'attributecode_1');
$product->setData('attributecode_2',$ConfiProduct->getData('attributecode_2'));
$product->getResource()->saveAttribute($product, 'attributecode_2');
$product->setData('attributecode_3',$ConfiProduct->getData('attributecode_3'));
$product->getResource()->saveAttribute($product, 'attributecode_3');
Related Topic