0

How I can stop ordering and redirecting to a page from the Observer with an error message.

    public function setSubscribeField($observer)
        {
            if(Mage::app()->getRequest()->isPost()){
                $postData = Mage::app()->getRequest()->getPost();
                $session = Mage::getSingleton('checkout/session');
                if(isset($postData['mailchimp_subscription'])){
                    $session->setData('mailchimp_subscription',$postData['mailchimp_subscription']);
                }else {
                    if($postData['mailchimp_subscription'] != 'on' && Mage::helper('subscription')->isRequired()){
   ????????
                    }
                }
            }

            return $this;
        }

If this parameter is required, than I must redirect to page with form and show error message . Thanks in advance.

2 Answers 2

1

I am only 90% sure that the redirect will work because I have only used it in controllers (not observers), but if it does, the error message should display at the top of the page like any other native Magento errors.

public function setSubscribeField($observer)
    {
        if(Mage::app()->getRequest()->isPost()){
            $postData = Mage::app()->getRequest()->getPost();
            $session = Mage::getSingleton('checkout/session');
            if(isset($postData['mailchimp_subscription'])){
                $session->setData('mailchimp_subscription',$postData['mailchimp_subscription']);
            }else {
                if($postData['mailchimp_subscription'] != 'on' && Mage::helper('subscription')->isRequired()){

// add your error to the session
                    $session->addError('[YOUR ERROR MESSAGE]');
// redirect to the page that the user came from 
                    $this->_redirect('*/*/');
                }
            }
        }

        return $this;
    }

Note:

The session that you have already pulled with $session = Mage::getSingleton('checkout/session'); is not the only type of session that can be used in this scenario. So, if the redirect works and the error does not, try

Mage::getSingleton('customer/session')->addError('[YOUR ERROR MESSAGE]');

or

Mage::getSingleton('core/session')->addError('[YOUR ERROR MESSAGE]');

For more on session-types take a look here

1
0

Redirection in observer doesn’t work normally as it do in Blocks, templates and controllers. Also there is no standard code to redirect from observer that works in every situation.

You will need an argument to achieve redirect when using below code:

public function observingMethod(Varien_Event_Observer $observer)
{
    $observer->getRequest()->setParam('return_url',$urlToRedirect);
}

Note that $observer object should have getRequest() method to make above code work. You may need to use $observer->getEvent()->getFront()->getRequest() otherwise, or simply var_dump/Mage::log $observer to get better idea what methods the object have.

Or you can use below code which is not recommended:

public function observingMethod() {
    header('Location: ' . $urlToRedirect);
    exit;
}

We don’t need any arguments using above method.

Another approach, again not recommended:

public function observingMethod(Varien_Event_Observer $observer)
{    
    $response = $observer->getResponse();
    //$response = Mage::app()->getFrontController()->getResponse();
    $response->setRedirect($urlToRedirect);

    Mage::app()->getFrontController()->sendResponse();
}

Link: https://web.archive.org/web/20211204202657/http://ka.lpe.sh/2013/07/21/magento-redirect-from-observer/

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.