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){
        ...
    }    
}
PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80

2 Answers2

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

sv3n
  • 11,657
  • 7
  • 40
  • 73
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.

Aman Alam
  • 1,318
  • 10
  • 25