0

I have some issue in my Event Observer in config.xml whose the class doesn't work:

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Prince_Cart>
            <version>0.0.1</version>
        </Prince_Cart>
    </modules>
    <global>
        <models>
            <cart>
                <class>Prince_Cart_Model</class>
            </cart>
        </models>
        <resources>
            <prince_cart_setup>
                <setup>
                    <module>Prince_Cart</module>
                </setup>
            </prince_cart_setup>
        </resources>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch_checkout_onepage_index>
                <observers>
                    <Prince_Cart_Model_Observer>
                        <type>singleton</type>
                        <class>prince_cart/observer</class> <!--This doesn't work-->
                        <!--<class>Prince_Cart_Model_Observer</class>--> <!--This works but is not a good practice! -->
                        <method>blockProceedToCheckout</method>
                    </Prince_Cart_Model_Observer>
                </observers>
            </controller_action_predispatch_checkout_onepage_index>
        </events>
    </frontend>
</config>

Another detail: in which case we know that we can put <type>singleton</type> or <type>model</type> or nothing ?

Observer.php

class Prince_Cart_Model_Observer extends Varien_Event_Observer {

    public function blockProceedToCheckout($observer){
        ...
    }    
}
2

Just this seems wrong:

<class>prince_cart/observer</class>

It should fit to your block declaration:

<models>
    <cart>
        <class>Prince_Cart_Model</class>
    </cart>
</models>

Either change your observer class to:

<class>cart/observer</class>

or declare your block with prince_cart:

<models>
    <prince_cart>
        <class>Prince_Cart_Model</class>
    </prince>
</models>

If you remove <type>...</type>, it will be called as singleton by default. For answering singleton or model this may help: Magento Event Observers: Singleton versus Model

0

Your xml should look like this

<?xml version="1.0"?>
<config>
    <modules>
        <Prince_Cart>
            <version>0.0.1</version>
        </Prince_Cart>
    </modules>
    <global>
        <helpers>
          <cart>
            <class>Prince_Cart_Helper</class>
          </cart>
        </helpers>
        <models>
            <cart>
                <class>Prince_Cart_Model</class>
            </cart>
        </models>
        <resources>
            <prince_cart_setup>
                <setup>
                    <module>Prince_Cart</module>
                </setup>
            </prince_cart_setup>
        </resources>
        <events>
            <controller_action_predispatch_checkout_onepage_index> <!-- identifier of the event we want to catch -->
                <observers>
                    <controller_action_predispatch_checkout_onepage_index_handler>
                        <type>model</type>
                        <class>cart/observer</class>
                        <method>blockProceedToCheckout</method>
                    </controller_action_predispatch_checkout_onepage_index_handler>
                </observers>
            </controller_action_predispatch_checkout_onepage_index>
        </events>
    </global>
</config>

Your Prince/Cart/Model/Observer.php should look like this :

<?php
class Prince_Cart_Model_Observer
{
    public function blockProceedToCheckout(Varien_Event_Observer $observer)
    {
        //Your logic
    }
}

Your Prince/Cart/Helper/Data.php should look like this :

<?php
class Prince_Cart_Helper_Data extends Mage_Core_Helper_Abstract
{

}

Regarding observer type :

model: a new instance of the observer class is instantiated for each event and the observer method of that instance is executed.

singleton: a single instance of the observer class is instantiated and the observer method of that single instance is executed for each event.

  • Thanks for you time, but why all this ? the helper ok I had it I just removed it for stack but why you replace the <Prince_Cart_Model_Observer> by <controller_action_predispatch_checkout_onepage_index_handler> and the <events>was in <frontend> ? – PЯINCƏ Jun 25 '17 at 2:08
  • just for naming conventions nothing else – Aman Alam Jun 25 '17 at 6:39
  • You can use any unique identifier instead – Aman Alam Jun 25 '17 at 6:40
  • Why to remove extends Varien_Event_Observer? – sv3n Jun 25 '17 at 9:26

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.