1

with the help I received here yesterday, I'm trying to set up an observer that will update the "Manage Stock" option of all child items of a configurable product when it is changed. Following quite a few tutorials, I think I have the code right, but the observer isn't firing. My module is setup as follows:

The module setup in /app/etc/modules: MyCompany_Catalog.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyCompany_Catalog>
            <active>true</active>
            <codePool>local</codePool>
        </MyCompany_Catalog>
    </modules>
</config>

The config setup in /app/code/local/MyCompany/Catalog/etc: config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyCompany_Catalog>
            <version>1.0.0</version>
        </MyCompany_Catalog>
    </modules>
    <stores>
        <admin>
            <design>
                <theme>
                    <template>MyCompany</template>
                </theme>
            </design>
        </admin>
    </stores>
    <global>
        <models>
            <mycompany_catalog>
                <class>MyCompany_Catalog_Model</class>
            </mycompany_catalog>
        </models>
        <events>
            <cataloginventory_stock_item_save_before>
                <observers>
                    <mycompany_catalog>
                        <class>mycompany_catalog/observer</class>
                        <method>mc_update_child</method>
                    </mycompany_catalog>
                </observers>
            </cataloginventory_stock_item_save_before>
        </events>
    </global>
</config>

And finally, the observer in /app/code/local/MyCompany/Catalog/Model: Observer.php

<?php

class MyCompany_Catalog_Model_Observer
{
    public function mc_update_child (Varien_Event_Observer $observer)
    {
        Mage::log('test');
    }
}

The module is working as far as the design is concerned, as it is pulling in a custom 'inventory.phtml' that I created under /app/design/adminhtml/default/MyCompany/template/catalog/product/tab However, when I save a configurable parent, nothing is being written to the system.log (should be logging 'test'). I hope it's something silly, like I mistyped something or I'm calling the wrong observer, and an extra set of eyes can spot my mistake. TIA.

2

Update the model to not use the shortcode in the event:

<events>
    <cataloginventory_stock_item_save_before>
        <observers>
            <mycompany_catalog>
                <class>MyCompany_Catalog_Model_Observer</class>
                <method>mc_update_child</method>
            </mycompany_catalog>
        </observers>
    </cataloginventory_stock_item_save_before>
</events>
  • Thanks for the reply. I modified the config.xml to match what you posted and tried it with both the 'model' section present and removed from the XML, but it still doesn't seem to be firing. Nothing being logged and I even removed a semi-colon from the Observer, hoping that it would throw an error. – John Mar 19 '14 at 17:07
  • Sounds like your module is not being registered - can you remove the </config> on your config.xml so as to make it invalid? Enable developer mode and refresh - you should see a simplexml error – philwinkle Mar 19 '14 at 17:32
  • Yes, doing that and reloading the page generated an error: Warning: simplexml_load_string(): Entity: line 33: parser error : Premature end of data in tag config line 2 in /var/www/magento-www/docs/lib/Varien/Simplexml/Config.php on line 510 – John Mar 19 '14 at 17:53
  • 1
    It seems that the "cataloginventory_stock_item_save_before" event wasn't happening. I changed the observer to fire on the "cataloginventory_stock_item_save_after" event, and it started writing to the log file (as it is only supposed to do at this time). As long as I can have it update the children using "_after", I can get this task done and have some time to learn more about when specific events run. – John Mar 19 '14 at 18:16
  • I see - I didn't validate the presence of the actual event itself. Good catch - the event needs to actually fire to observe it ;) – philwinkle Mar 19 '14 at 18:17

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.