0

I have followed this example magento: URL querystring for adding product and applying discount coupon to cart to add products to cart and apply discounts at the same time using 1 request.

It works fine except for the first request (ex: in incognito mode) as it will give a 404 not found and then if I refresh the URL again it will work just fine.

The URL looks like this

https://www.website.com/checkout/cart/add/?product=10&qty=1&return_url=https://www.website.com/index.php/checkout/cart/couponPost/?coupon_code=discount

I believe this comes from the fact that the session is not initialized at the moment of the request so I tried to add a redirect in the code but it is not working and also it does not seem like the brightest idea.

This is the code that adds the cart function

require_once 'Mage/Checkout/controllers/CartController.php';

    class Namespace_AddProductFromUrl_Checkout_CartController extends Mage_Checkout_CartController {
        # overloaded addAction
        public function addAction()
        {        
            // generate form_key if missing or invalid
            if ( ! ($formKey = $this->getRequest()->getParam('form_key', null)) or $formKey != Mage::getSingleton('core/session')->getFormKey())
            {
                $this->getRequest()->setParams(array('form_key' => Mage::getSingleton('core/session')->getFormKey()));
            }        

            // do parent actions
            parent::addAction();
        }
    }

What I tried so far is to add a redirect in the main if:

$this->getRequest()->setParams(array('form_key' => Mage::getSingleton('core/session')->getFormKey()));
$this->_redirect($this->getRequest()->getRequestUri());
Mike
  • 3,017
  • 1
  • 34
  • 47

1 Answers1

0

I found a fix for this but it is only partial for now.

The redirect issue comes form the cookie verification, so going to Config > General > Web > Browser Capability Detection > Redirect to CMS-page if Cookies are Disabled set to no would sort out the first part.

I also tried to replicate this on a fresh system to make sure it is not from a conflict of some sort and it seems that the module declaration for M 1.9.4.1 is not working.

I can see it in Config > Advanced > Advanced but it does not alter the addAction() function.

Therefore I have edited CartController.php at line 209 in app/code/core/Mage/Checkout/controllers/ and added this part:

if ( ! ($formKey = $this->getRequest()->getParam('form_key', null)) or $formKey != Mage::getSingleton('core/session')->getFormKey())
            {
                $this->getRequest()->setParams(array('form_key' => Mage::getSingleton('core/session')->getFormKey()));
            } 

Now it works properly.

What remains to be done is either create a copy of this in the community folder or a proper module declaration.

Mike
  • 3,017
  • 1
  • 34
  • 47