Viewed   69 times

In this event checkout_cart_add_product_complete, I want the customer to be redirected to an external web page http://www.example.com/. For this I am using this code, which is not working at all:-

public function moduleMethod() {
    /* @var $response1 Mage_Core_Controller_Response_Http */
    $response1 = $observer->getEvent()->getResponse();

    /* @var $response2 Mage_Core_Controller_Response_Http */
    $response2 = Mage::app()->getResponse();

    $url = 'http://www.example.com/';
    $response1->setRedirect($url);

    return;
}

I have used the "setRedirect()" method on both these variables "$response1" and "$response2", but both of them show me the Shopping Cart page, whereas I want to see this page http://www.example.com/ instead.

What I want:

  • I don't want to override the Controller Class, just to redirect the Customer, when I can effectively use the Event Observer process.
  • I don't want to use the PHP in-built function "header()", when Magento framework provides this functionality in an efficient way.

 Answers

4

tl;dr: Correct observer code at the bottom.

A note before I answer: Make sure that the observer is being triggered; step through your code or use die('here');. As written, your example method does not have the correct prototype to receive event observer data (missing an argument).

Using an event observer for redirect logic is totally appropriate in this context, as evinced by the core team explicitly passing the request and response objects in to the observer. Your attempt is good, but I think that you have conditions and configuration which cause execution to flow to Mage_Checkout_CartController::_goBack(), specifically to the line

$this->_redirect('checkout/cart');

So we need to revise our approach. Now, you could prevent any request/response logic from processing after your event observer by manipulating the response and calling the Front Controller's sendResponse() method as demonstrated below (nb: don't do this!):

public function moduleMethod($observer) //note I added a param
{
    /* @var $response1 Mage_Core_Controller_Response_Http */
    $response1 = $observer->getResponse(); // observers have event args

    $url = 'http://www.example.com/';
    $response1->setRedirect($url);

    /* SHOULDN'T DO THIS */
    Mage::app()->getFrontController()->sendResponse();
}

This should work, but I think it mixes up areas of concern by triggering output from an ethereal system component (EDA). Let's see if there's something in the command-control structure which we can use...

Just after the checkout_cart_add_product_complete event execution comes to the cart controller's _goBack() method. The problem with this method name is that it does more than its name implies:

/**
 * Set back redirect url to response
 *
 * @return Mage_Checkout_CartController
 */
protected function _goBack()
{
    $returnUrl = $this->getRequest()->getParam('return_url');
    if ($returnUrl) {
        // clear layout messages in case of external url redirect
        if ($this->_isUrlInternal($returnUrl)) {
            $this->_getSession()->getMessages(true);
        }
        $this->getResponse()->setRedirect($returnUrl);
    }
    //...
}

It looks like we can just set a return_url param on the request object and accomplish what we need.

public function moduleMethod(Varien_Event_Observer $observer)
{
    $observer->getRequest()->setParam('return_url','http://www.google.com/');
}

I've tested this, and it should do the trick!

Tuesday, September 13, 2022
2

In Magento, you can encrypt the data using:

Mage::getModel('core/encryption')->encrypt($data);

and decrypt using:

Mage::getModel('core/encryption')->decrypt($data);
Friday, October 7, 2022
 
amcnabb
 
5

Here is what is happening. Your configuration of the admin controller is wrong. The proper way to include your controller in the admin section is this (you'll need to change out the module name, to match yours):

<?xml version="1.0" ?>

<config>
    <modules>
        <Video_Awesome>
            <version>0.0.1</version>
        </Video_Awesome>
    </modules>
    <global>
        <models>
            <Video_Awesome>
                <class>Video_Awesome_Model</class>
                <!-- No resource model used currently -->
            </Video_Awesome>
        </models>
        <events>
            <controller_action_predispatch>
                <observers>
                    <controller_action_before>
                        <type>singleton</type>
                        <class>Video_Awesome/Observer</class>
                        <method>controllerActionPredispatch</method>
                    </controller_action_before>
                </observers>
            </controller_action_predispatch>
            <upload_video_before>
                <observers>
                    <Video_Awesome>
                        <type>singleton</type>
                        <class>Video_Awesome/Observer</class>
                        <method>uploadVideoBefore</method>
                    </Video_Awesome>
                </observers>
            </upload_video_before>
        </events>
    </global>

    <admin>
        <routers>
            <adminhtml>
            <!-- we are not creating our own router, but tapping into the adminhtml router -->
                <args>
                    <modules>
                        <!-- Your module name, and then
                        the path to the controller (minus
                        the controllers folder name). So,
                        in this instance, I put the router
                        in a "Adminhtml" folder inside of
                        the controllers folder, like thus:
        Video/Awesome/controllers/Adminhtml/videotestimonialsController.php -->
                        <Video_Awesome before="Mage_Adminhtml">Video_Awesome_Adminhtml</Video_Awesome>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

And, then, in your router (you don't need to call getEvent before getting the controller action):

public function controllerActionPredispatch ($observer)
{
    if($observer->getControllerAction()->getFullActionName() == 'adminhtml_videotestimonials_post') {
        // dispatching our own event before action upload video is run and sending parameters we need
    Mage::dispatchEvent("upload_video_before", array('request' => $observer->getControllerAction()->getRequest()));
    }
}

And finally, it doesn't sound as if you have a debugging setup for your Magento development. I would highly recommend one. I use PHPStorm (I don't have any stake in the company - this is not an advertisement :), and it works awesome. Set a breakpoint there to see if what the variables are.

I would also recommend using adminhtml_controller_action_predispatch_start, instead of the global controller_action_predispatch, as it will only trigger in the backend (a very, very small performance difference).

Also, as a small sidenote, I saw in your config.xml, that you were specifying menu items/acl there. You probably didn't know, but that is deprecated functionality, and those items should be put it adminhtml.xml.

Sunday, August 21, 2022
 
4

Keep in mind that the success page doesn't necessarily change the payment state to approved. This is because different payment methods may approve a payment at different times. For example, Paypal will not approve the payment until it has a chance to process it.

Does your CC company provide callbacks that you can use to update the status? If so, I suggest using the Paypal module as a template for how to handle this (wait for the callback, update the order status). If not, perhaps use a cronjob and their API to check the payment status.

Overall, do not depend on customers visiting a certain page after they have paid, as there are plenty of situations where this will not be the case.

Hope that helps!

Thanks, Joe

Wednesday, December 14, 2022
 
cay
 
cay
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :