7

I need to override a core controller's preDispatch method. The core controller inherits from Mage_Checkout_Controller_Action, which itself has a preDispatch method that needs to be called. Since I'm extending from the core controller I want to override, I cannot just call parent::preDispatch(), since that would pull in functionality that I'm trying to avoid.

My question is - How can I call an overridden method that is 2 levels higher in the inheritance chain, or am I just going about this the wrong way?

Update

Apparently this works:

Mage_Checkout_Controller_Action::preDispatch(); (even though that method is not static, I guess you learn something new every day..)

Now my question is: Is there a better way of doing this without having to copy in all functionality from the class I am overridding?

The reason I am attempting this is because I was going down this path, attempting to force Magento to remember address assignments when customers go through multishipping checkout and then subsequently leave. (FWIW, I found the linked to article great for getting me going in the right direction, but there seems to be a lot missing as far as steps that need to be taken to get this to work).

The controller I'm overriding is the core MultiShippingController which has a redirect back to the Multishipping IndexAction that needs to be avoided. On a somewhat side-note, I have gotten this functionality mostly complete, save for a few issues (relating to the adding of the first item to the cart and the Mage_Sales_Model_Quote_Item->getQuoteItem() returning null in the addresses.phtml template, though that is an entirely different discussion).

  • 1
    Just in case anyone else wanders onto this later and wonders how/why the (seemingly) static syntax is working. I'd refer you to this StackOverflow answer which explains it very well. In short, it's not "static" just like parent::someMethod() is not static, but rather specifies part of the inheritance tree to traverse before calling a method in the proper $this context of the present class' instance. – Brian Nov 18 '15 at 2:09
8

I think it'd be interesting to know exactly what you're trying to accomplish, but I can give you a few pointers without knowing details:

In most cases if the method you're trying to reference does not use $this, you can safely call it statically:

Mage_Checkout_Controller_Action::preDispatch()

But you should probably be doing this via a controller predispatch observer. Magento affords you the ability to do this in Mage_Core_Controller_Varien_Action:

abstract class Mage_Core_Controller_Varien_Action
{
    // [...]
    public function preDispatch()
    {
        // [...]
        if ($this->_rewrite()) {
            return; // [What is the purpose if this?]
        }
        // [...]

        // [This is where my event needs to be firing, but this code never gets 
        // executed because the controller is rewritten]
        Mage::dispatchEvent(
            'controller_action_predispatch_'.$this->getFullActionName(),
            array('controller_action'=>$this)
        );

    }
    // [...]
}

All you have to do is add the full action name onto the end of controller_action_predispatch. An example of shoehorning functionality into Onepage Savebilling:

    <events>
        <controller_action_predispatch_checkout_onepage_saveBilling>
            <observers>
                <some_unique_name_for_this_event>
                    <class>yourmodel/observer</class>
                    <method>myPredispatchObserver</method>
                </some_unique_name_for_this_event>
            </observers>
        </controller_action_predispatch_checkout_onepage_saveBilling>
    </events>

What's more, in your observer method, you need to accept the observer and you can get the controller itself from getEvent:

<?php

class MyCompany_MyModule_Model_Observer
{

    public function myPredispatchObserver($observer)
    {
        $controller = $observer->getEvent()->getControllerAction();
    }
}
  • 1
    Thanks for the insight. I updated my question as to why I'm doing this. – Jeff Lambert Jul 11 '13 at 13:54
1

I'm on my phone, but I'd think that you could use any one of several events to remove the redirect from the response object.

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.