从Controller中的preDispatch传递发布数据

时间:2015-09-24 13:49:23

标签: php magento controller checkout magento-1.9

我在购物车页面上有一个自定义表单,用户可以在其中输入特殊代码。为了确保用户登录,我在控制器中包含了一个preDispatch方法。

public function preDispatch() {
    parent::preDispatch();

    $action = $this->getRequest()->getActionName();
    $loginUrl = Mage::helper('customer')->getLoginUrl();

    if (!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)) {
        $this->_getCoreSession()->addSuccess('Sie müssen sich einloggen oder ein Benutzerkonto erstellen um Ihren Gutschein zu aktivieren.');
        $this->setFlag('', self::FLAG_NO_DISPATCH, true);
    }
}

preDispatch正在运行。当用户输入代码并且未登录时,他将被重定向到登录页面并成功登录购物车后。 现在处理表单值的帖子的函数没有得到输入。 我按如下方式检索输入:

public function activateGiftCardCreditAction() {
    // action handles post request when customer activates gift card

    $giftCardCode = trim((string)$this->getRequest()->getParam('giftcardcredit_code'));
    $amount = (float)$this->getRequest()->getParam('giftcard_amount');
    $card = Mage::getModel('giftcards/giftcards')->load($giftCardCode, 'card_code');

    // do something with form values ...

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

如何通过登录页面传递表单值?

1 个答案:

答案 0 :(得分:1)

preDispatch()

会话变量 中保存帖子数据的想法
    Mage::getSingleton('customer/session')->
setBeforeGiftCardRequest($this->getRequest()->getPost());

然后 发布该会话变量的数据

    /* add this code */
     $session = Mage::getSingleton('customer/session');
    $requestParams = $this->getRequest()->getParams();
    if ($session->getBeforeGiftCardRequest()) {
        $requestParams = $session->getBeforeGiftCardRequest();
        $session->unsBeforeGiftCardRequest();
    }

所以试试吧:

 public function preDispatch() {
    parent::preDispatch();

    $action = $this->getRequest()->getActionName();
    $loginUrl = Mage::helper('customer')->getLoginUrl();

    if (!Mage::getSingleton('customer/session')->authenticate($this, $loginUrl)) {
        $this->_getCoreSession()->addSuccess('Sie müssen sich einloggen oder ein Benutzerkonto erstellen um Ihren Gutschein zu aktivieren.');
		// Assign post value in customer/session variable 
		 Mage::getSingleton('customer/session')->setBeforeGiftCardRequest($this->getRequest()->getParams());
        $this->setFlag('', self::FLAG_NO_DISPATCH, true);
    }
}

public function activateGiftCardCreditAction() {
	
				/* add this code */
			 $session = Mage::getSingleton('customer/session');
            $requestParams = $this->getRequest()->getParams();
            if ($session->getBeforeGiftCardRequest()) {
                $requestParams = $session->getBeforeGiftCardRequest();
                $session->unsBeforeGiftCardRequest();
            }
    // action handles post request when customer activates gift card

    $giftCardCode = trim((string)$this->getRequest()->getParam('giftcardcredit_code'));
    $amount = (float)$this->getRequest()->getParam('giftcard_amount');
    $card = Mage::getModel('giftcards/giftcards')->load($giftCardCode, 'card_code');

    // do something with form values ...

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