Search code examples
phpmagentocontrollercheckoutmagento-1.9

Pass post data from preDispatch in Controller


I have a custom form on the cart page, where users can enter special codes. To make sure, the user is logged in i included a preDispatch method in the controller.

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);
    }
}

The preDispatch is working. When the user enters a code and isn't logged in, he is redirected to the login page and after successful login back to cart. Now the function handling the post of the form value doesn't get the input. I'm retrieving the input as following:

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');
}

How can i pass the form values through the login page?


Solution

  • As an idea to save post data in a session variable at preDispatch()

        Mage::getSingleton('customer/session')->
    setBeforeGiftCardRequest($this->getRequest()->getPost());
    

    and then post data from that session variable

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

    So let try:

     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);
        }
    }

    and

    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');
    }