Skip to content

Commit 9f1f584

Browse files
author
Mohan Ahuja
committed
ACP2E-711: "Invalid Form Key. Please refresh the page." error occurs when changing the product limit in categories
- Updated solution to pass functional test cases, and run solution only when the setting is set.
1 parent fb31225 commit 9f1f584

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

app/code/Magento/PageCache/ViewModel/FormKeyProvider.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\PageCache\ViewModel;
99

10+
use Magento\Framework\App\Config\ScopeConfigInterface;
1011
use Magento\Framework\View\Element\Block\ArgumentInterface;
1112
use Magento\PageCache\Model\Config;
1213

@@ -15,18 +16,31 @@
1516
*/
1617
class FormKeyProvider implements ArgumentInterface
1718
{
19+
/**
20+
* XML PATH to enable/disable saving toolbar parameters to session
21+
*/
22+
const XML_PATH_CATALOG_REMEMBER_PAGINATION = 'catalog/frontend/remember_pagination';
23+
1824
/**
1925
* @var Config
2026
*/
21-
private $config;
27+
private Config $config;
28+
29+
/**
30+
* @var ScopeConfigInterface object
31+
*/
32+
private ScopeConfigInterface $scopeConfig;
2233

2334
/**
2435
* @param Config $config
36+
* @param ScopeConfigInterface $scopeConfig
2537
*/
2638
public function __construct(
27-
Config $config
39+
Config $config,
40+
ScopeConfigInterface $scopeConfig
2841
) {
2942
$this->config = $config;
43+
$this->scopeConfig = $scopeConfig;
3044
}
3145

3246
/**
@@ -38,4 +52,9 @@ public function isFullPageCacheEnabled(): bool
3852
{
3953
return $this->config->isEnabled();
4054
}
55+
56+
public function isPaginationCacheEnabled(): bool
57+
{
58+
return $this->scopeConfig->isSetFlag(self::XML_PATH_CATALOG_REMEMBER_PAGINATION);
59+
}
4160
}

app/code/Magento/PageCache/view/frontend/templates/form_key_provider.phtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ if ($block->getFormKeyProvider()->isFullPageCacheEnabled()): ?>
77
<script type="text/x-magento-init">
88
{
99
"*": {
10-
"Magento_PageCache/js/form-key-provider": {}
10+
"Magento_PageCache/js/form-key-provider": {
11+
"isPaginationCacheEnabled": <?= /* @noEscape */ $block->getFormKeyProvider()->isPaginationCacheEnabled() ?>
12+
}
1113
}
1214
}
1315
</script>

app/code/Magento/PageCache/view/frontend/web/js/form-key-provider.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
define(function () {
66
'use strict';
77

8-
return function () {
8+
return function (settings) {
99
var formKey,
1010
inputElements,
1111
inputSelector = 'input[name="form_key"]';
@@ -82,8 +82,13 @@ define(function () {
8282
function initFormKey() {
8383
formKey = getFormKeyCookie();
8484

85+
if (settings.isPaginationCacheEnabled && !formKey) {
86+
formKey = getFormKeyFromUI();
87+
setFormKeyCookie(formKey);
88+
}
89+
8590
if (!formKey) {
86-
formKey = getFormKeyFromUI() || generateFormKeyString();
91+
formKey = generateFormKeyString();
8792
setFormKeyCookie(formKey);
8893
}
8994
inputElements = document.querySelectorAll(inputSelector);

0 commit comments

Comments
 (0)