3

Can you see why my observer is not firing?

config.xml:

  <?xml version="1.0" encoding="UTF-8"?>
<config>    
    <modules>
        <Caitlinhavener_Dynamicprice>
            <version>0.1.0</version>
        </Caitlinhavener_Dynamicprice>
    </modules>
    <global>
        <models>
            <chdispatcher>
                <class>Caitlinhavener_Dynamicprice_Model</class>
            </chdispatcher>
        </models>

        <frontend>
            <events>
                <checkout_cart_product_add_after>
                    <observers>
                        <modify_to_custom_price>
                            <class>Caitlinhavener_Dynamicprice_Model_Observer</class>
                            <method>modifyPrice</method>
                        </modify_to_custom_price>
                    </observers>
                </checkout_cart_product_add_after>
            </events>
        </frontend>
    </global>
</config>

Observer.php:

    <?php
Mage::log('Im here')
or exit("unable to log");
class Caitlinhavener_Dynamicprice_Model_Observer
{
    public function modifyPrice(Varien_Event_Observer $obs)
    {
        // Get the quote item
        $item = $obs->getQuoteItem();
        Mage::log('Get Quote Item '. var_dump($_item->debug());

        // Ensure we have the parent item, if it has one
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        Mage::log('Get parent item ' . var_dump($_item->debug());

        // Load the custom price
        //$price = "your custom price logic";
        $price = Mage::registry('dynamic_tier_price');
        Mage::log('Price is ' . $price);

        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
        Mage::log('Item after super mode ' . var_dump($_item->debug());
    }



}
?>

Both @airhorse and @philwinkle had the correct answers. I had to do all of these things for it to work.

6
  • coding.smashingmagazine.com/2012/03/01/basics-creating-magento-module/ has the classname all lowercase like that in config so that's how I did it, but I'm seeing it out there other ways.. Jun 5 '13 at 15:09
  • Is your module registered in app/etc/modules?
    – philwinkle
    Jun 5 '13 at 15:10
  • e.g. Caitlinhavener_Dynamicprice.xml
    – philwinkle
    Jun 5 '13 at 15:10
  • 1
    Yes and I've verified in system>config>advanced > list of modules Jun 5 '13 at 15:13
  • I added what you see above. Jun 5 '13 at 16:15
9

A few things:

The model is defined as:

<class>Caitlinhavener_Dynamicprice_Model_Observer</class>

Which is looking for the class file in the folder structure Caitlinhavener/Dynamicprice/Model/Observer/Observer.php.

Change this line to:

<class>Caitlinhavener_Dynamicprice_Model</class>

Likewise, you have an issue in the model/method in your event listener - change to:

<class>chdispatcher/observer</class>

Edit:

Frontend should not be nested inside of <global>:

<global>
    <models>
        <chdispatcher>
            <class>Caitlinhavener_Dynamicprice_Model</class>
        </chdispatcher>
    </models>
</global>


<frontend>
    <events>
        <checkout_cart_product_add_after>
            <observers>
                <modify_to_custom_price>
                    <class>chdispatcher/observer</class>
                    <method>modifyPrice</method>
                </modify_to_custom_price>
            </observers>
        </checkout_cart_product_add_after>
    </events>
</frontend>
12
  • Also, consider renaming the dispatcher class name to chdispatcher or something not so generic and potentially namespace conflicting.
    – philwinkle
    Jun 5 '13 at 15:23
  • So I made your changes.. and I added something to cart then went to my var/log/system.log to see if any of my Mage::logs did anything. There's nothing. Does that mean its still not firing? Jun 5 '13 at 15:27
  • Did you clear cache, etc.? Change the Mage::log('Im here'); to die(), etc. and ensure that logging is enabled in System > Config > Developer
    – philwinkle
    Jun 5 '13 at 15:33
  • Cleared cache, logging is enabled, triple verified that the module is enabled.. I'm not sure how to use die... still not working. Jun 5 '13 at 15:39
  • I'm reading php docs for die/exit.. I'll try Jun 5 '13 at 15:41
2

You need to redeclare your models and take your events tag out of the frontend tag. See below.

<config>    
   <modules>
       <Caitlinhavener_Dynamicprice>
           <version>0.1.0</version>
        </Caitlinhavener_Dynamicprice>
   </modules>
   <global>
      <models>
           <dispatcher>
               <class>Caitlinhavener_Dynamicprice_Model</class>
           </dispatcher>
       </models>
       <events>
            <checkout_cart_product_add_after>
                <observers>
                    <modify_to_custom_price>
                        <class>caitlinhavener_dynamicprice/observer</class>
                        <method>modifyPrice</method>
                    </modify_to_custom_price>
                </observers>
            </checkout_cart_product_add_after>
       </events>
    </global>
</config>
3
  • 2
    Having events in frontend isn't wrong.
    – philwinkle
    Jun 5 '13 at 15:21
  • He's right about front end. Worked when I took it out. Jun 5 '13 at 17:39
  • 2
    Having events in frontend isn't wrong - having <frontend> in the wrong location, is.
    – philwinkle
    Jun 5 '13 at 17:54

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.