magento2: Add to Cart Form wrong Form Key in FPC
When the page initially loads the formkey that is placed on the page is cached between independent requests. This is not an issue if the entire page loads before the “Add to Cart” button is pressed, however if the Add to Cart is clicked before the Javascript has fully initialized (and therefore the form is actually posted via a standard POST HTTP request) then the formkey that is sent in the form data does not match the users session and the product is not added to the cart.
Preconditions
- Magento CE 2.2.2 with sample data installed
- Full Page Cache enabled
- Redirect to cart on “Add to Cart” set to yes in configuration (Sales>Checkout>Shopping Cart)
Steps to reproduce
- Open a browser window and navigate to a product page.
- View the page source and search for the formkey that is part of product_addtocartform form (form id)
- Note the form_key down
- Open an incognito window and navigate to the same product page
- View the page source again and search for the form_key field
Expected result
- The two form_key values should be different
Actual result
- The two form_key values are the same (meaning the second one is wrong as it will not match the cookie)
Additional information
- detailed explanation https://github.com/magento/magento2/issues/13746#issuecomment-368062429
- confirmed for
2.4-develop
https://github.com/magento/magento2/issues/13746#issuecomment-570216197
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 8
- Comments: 39 (14 by maintainers)
Any update?
Using developer tools won’t demonstrate the problem as the Javascript updates the developer tools and this problem is only apparent before Javascript has fully executed. You need to view the page source (i.e. in chrome using “view-source:”, which does not execute Javascript) to see the initial form_key that is loaded with the page (and is therefore used if the Add to Cart button is clicked before the Javascript has executed).
The issue was fixed in https://github.com/magento/magento2/pull/30961. You can apply it as a patch
@jonathanribas ah, sorry, I just looked at the wrong issue. This issue was fixed in https://github.com/magento/magento2/pull/30961