2

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');
        }
    }
  • Mind that by default Magento displays the information from the configurable product so that you normally do not need to do that. – Simon Aug 13 '14 at 14:56
  • Right. What this is for is their GoDataFeed information. The feeds are pulling the associated product values for their custom price attributes instead of the configurable product. So they're having to change the price in a bunch of different places. – Joshua Romero Aug 13 '14 at 15:05
  • Then I would rather try to fix the GoDataFeed extension which obviously reads the wrong values from the products. – Simon Aug 13 '14 at 15:07
  • That's what I suggested. Alas, here we are. :) – Joshua Romero Aug 13 '14 at 15:10
  • If I understand correctly, than you did not. You try to solve the problem on the Magento side. Although the GoDataFeed extension has to be fixed - it has to get the attributes from the configurable product... – Simon Aug 13 '14 at 15:12
1

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');
  • That worked beautifully. Now I just need to extend that so if they update any of the three custom attributes they all get transferred to their corresponding associated products. – Joshua Romero Aug 13 '14 at 15:02
  • Yep. Thanks a lot Amit. You were a huge help. – Joshua Romero Aug 13 '14 at 15:16
  • I wish to update the value of the color attribute of the configurable product. Can you tell me the solution – Jaisa May 13 '18 at 16:09

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.