0

I need to set a new value to a custom product attribute.

I want this to happen when a new product is created. The default value of the attribute is 0, I need to set it to 1.

I have an event observer for catalog_product_load_after.

<?xml version="1.0"?>
<config>
    <modules>
        <Schott_ShowQuoteAtZero>
            <version>1.0.0</version>
        </Schott_ShowQuoteAtZero>
    </modules>
    <global>
        <events>
            <catalog_product_load_after>
                <observers>
                    <Schott_ShowQuoteAtZero_Observer>
                        <type>model</type>
                        <class>schott_showquoteatzero/observer</class>
                        <method>showQuoteButtonAtZeroPrice</method>
                    </Schott_ShowQuoteAtZero_Observer>
                </observers>
            </catalog_product_load_after>
        </events>
    </global>
</config> 

Here is my observer

class Schott_ShowQuoteAtZero_Model_Observer
    {
        public function showQuoteButtonAtZeroPrice(Varien_Event_Observer $observer)
        {
            $product = $observer->getEvent()->getProduct();
            $product->setData('quotemode_conditions',1);
            $product->save();
        }
    }

It does not change the attribute value. Can anyone help?

2

1 Answer 1

2

You can set the default value for your attribute in:

Admin -> Catalog -> Attribute -> Manage attributes

If you want to set the value when you create a product from backend, you can use catalog_product_new_action event.

When you use catalog_product_save_before event, please remove $product->save(); otherwise it will create an infinite loop.


Btw ... is it possible that your config.xml isn't complete? I miss the <models> node:

<models>
    <schott_showquoteatzero>
        <class>Schott_ShowQuoteAtZero_Model</class>
    </schott_showquoteatzero>
<models>
5
  • In admin it is not possible to edit the attribute (default value) Mar 23, 2018 at 14:49
  • catalog_product_new_action does not change a thing Mar 23, 2018 at 14:50
  • config is good, this structure works for other modules too. Mar 23, 2018 at 14:51
  • @SelfDiagnostics You're right, admin default value only works for text attributes. Without declared <models> it can not work ... Can you please add a Mage::log to you observer to ensure it is called?
    – sv3n
    Mar 23, 2018 at 15:11
  • sorry, I did not read your text carefully enough. You are right, it works, thank you! Mar 23, 2018 at 15:18

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.