【问题标题】:How can i redirect from Observer with Magento如何使用 Magento 从 Observer 重定向
【发布时间】:2013-11-26 17:22:30
【问题描述】:

我正在使用观察者来捕捉该事件:controller_action_postdispatch_checkout_onepage_saveBilling

我想要那个事件来检查客户的帐单地址。

我的问题是:此时我如何向用户发送消息。按下继续按钮时。

我使用的是:Mage::getSingleton('core/session')->addError('My message');

但在您刷新页面之前不会显示该消息。这是第二个问题。 我如何从那一点重定向页面? 我没有运气使用:$this->_redirect('*/*/'); 和没有运气的硬编码网址。 理想情况下,我希望当客户按下继续按钮时,在我进行一些检查后,通过一条消息停止该过程。检查观察者并且它按预期工作。

希望它有意义..

编辑 那是我的代码。只是为了确保我做对了: 模型/观察者

class Company_Restrictions_Model_Observer {

    public function notifyUser($observer) {
//Lot's off lines are tested here.Nothing seems to work..
$observer->getRequest()->setParam('return_url','http://www.google.com/');

}
}

config.xml

<?xml version="1.0"?>

<config>
  <modules>
      <Company_Restrictions>
          <version>0.1.0</version>
      </Company_Restrictions>
  </modules>
  <global>
    <models>
        <company_restrictions>
             <class>Company_Restrictions_Model</class>
        </company_restrictions>
    </models>
    <helpers>
        <company_restrictions>
             <class>Company_Restrictions_Helper</class>
        </company_restrictions>
    </helpers>
    <events>
      <controller_action_postdispatch_checkout_onepage_saveBilling>
        <observers>
          <notify_user>
            <type>singleton</type>
            <class>Company_Restrictions_Model_Observer</class>
            <method>notifyUser</method>
          </notify_user>
        </observers>
      </controller_action_postdispatch_checkout_onepage_saveBilling>     
    </events>
  </global>
</config>

再次编辑

调试后我发现很多函数都给我错误。未定义的方法...例如:_redirect() 或 Response() 或 Body().. 有人知道为什么吗??

【问题讨论】:

    标签: magento redirect magento-1.7 observers


    【解决方案1】:

    看看@Magento - Redirect Customer from Observer Method

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

    【讨论】:

    • 仍然无法正常工作..我试图捕捉的事件有问题吗?
    【解决方案2】:

    我有一个成功重定向的观察者,我只是使用 setRedirect 方法:

    public function moduleMethod(Varien_Event_Observer $observer)
    {
        // get the current front controller
        $frontController = $observer->getEvent()->getFront();
    
        if(.....)
        {           
             $newUrl = ...;
    
             $frontController->getResponse()->setRedirect($newUrl);
             Mage::app()->getResponse()->sendResponse();
             exit;
        }
    }
    

    【讨论】:

      【解决方案3】:

      仅供参考,我会给你我从 stackoverflow magento 得到的答案: 这就是我设法使它工作的方式..

      $controllerAction = $observer->getEvent()->getControllerAction();
      $result = array();
      $result['error'] = '-1';
      $result['message'] = 'YOUR MESSAGE HERE';
      $controllerAction->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
      

      Magento Stackoverflow

      【讨论】:

        【解决方案4】:

        此代码将起作用:

        $url = YOUR_URL; // e.g. Mage::getUrl('customer/account/login')
        Mage::app()->getResponse()
                    ->setRedirect($url)
                    ->sendResponse();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多