Skip to content

Commit 707be0d

Browse files
#39905 Product Removed on Mobile Still Appears in Web's Mini Compare Section Until Re-login
Introduced CompareCookieManager to manage cookie invalidation for compare list products. Updated resolvers to use this service and ensure cookies are marked invalid after product compare list operations. Removed redundant client-side cookie reload logic in the frontend.
1 parent 7b8032d commit 707be0d

File tree

5 files changed

+145
-13
lines changed

5 files changed

+145
-13
lines changed

app/code/Magento/Catalog/view/frontend/web/js/view/compare-products.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ define([
4646
customerData.reload(['compare-products'], false);
4747
compareProductsReloaded = true;
4848
}
49-
50-
// Force reload on page load
51-
customerData.reload(['compare-products'], false);
52-
5349
initSidebar();
5450
}
5551
});

app/code/Magento/CompareListGraphQl/Model/Resolver/AddProductsToCompareList.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2020 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
1011
use Magento\Catalog\Helper\Product\Compare;
1112
use Magento\Catalog\Model\MaskedListIdToCompareListId;
1213
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
@@ -51,26 +52,35 @@ class AddProductsToCompareList implements ResolverInterface
5152
*/
5253
private mixed $productCompareHelper;
5354

55+
/**
56+
* @var CompareCookieManager
57+
*/
58+
private CompareCookieManager $compareCookieManager;
59+
5460
/**
5561
* @param AddToCompareList $addProductToCompareList
5662
* @param GetCompareList $getCompareList
5763
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
5864
* @param GetListIdByCustomerId $getListIdByCustomerId
5965
* @param Compare|null $productCompareHelper
66+
* @param CompareCookieManager|null $compareCookieManager
6067
*/
6168
public function __construct(
6269
AddToCompareList $addProductToCompareList,
6370
GetCompareList $getCompareList,
6471
MaskedListIdToCompareListId $maskedListIdToCompareListId,
6572
GetListIdByCustomerId $getListIdByCustomerId,
66-
?Compare $productCompareHelper = null
73+
?Compare $productCompareHelper = null,
74+
?CompareCookieManager $compareCookieManager = null
6775
) {
6876
$this->addProductToCompareList = $addProductToCompareList;
6977
$this->getCompareList = $getCompareList;
7078
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
7179
$this->getListIdByCustomerId = $getListIdByCustomerId;
7280
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
7381
->get(Compare::class);
82+
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
83+
->get(CompareCookieManager::class);
7484
}
7585

7686
/**
@@ -116,6 +126,7 @@ public function resolve(
116126
try {
117127
$this->addProductToCompareList->execute($listId, $args['input']['products'], $context);
118128
$this->productCompareHelper->calculate();
129+
$this->compareCookieManager->invalidate();
119130
} catch (\Exception $exception) {
120131
throw new GraphQlInputException(__($exception->getMessage()));
121132
}

app/code/Magento/CompareListGraphQl/Model/Resolver/CreateCompareList.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2020 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
1011
use Magento\Catalog\Helper\Product\Compare;
1112
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
1213
use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService;
@@ -57,6 +58,11 @@ class CreateCompareList implements ResolverInterface
5758
*/
5859
private mixed $productCompareHelper;
5960

61+
/**
62+
* @var CompareCookieManager
63+
*/
64+
private CompareCookieManager $compareCookieManager;
65+
6066
/**
6167
* @param Random $mathRandom
6268
* @param GetListIdByCustomerId $getListIdByCustomerId
@@ -71,7 +77,8 @@ public function __construct(
7177
AddToCompareList $addProductToCompareList,
7278
GetCompareList $getCompareList,
7379
CreateCompareListService $createCompareList,
74-
?Compare $productCompareHelper = null
80+
?Compare $productCompareHelper = null,
81+
?CompareCookieManager $compareCookieManager = null
7582
) {
7683
$this->mathRandom = $mathRandom;
7784
$this->getListIdByCustomerId = $getListIdByCustomerId;
@@ -80,6 +87,8 @@ public function __construct(
8087
$this->createCompareList = $createCompareList;
8188
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
8289
->get(Compare::class);
90+
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
91+
->get(CompareCookieManager::class);
8392
}
8493

8594
/**
@@ -123,6 +132,7 @@ public function resolve(
123132
$listId = $this->createCompareList->execute($generatedListId, $customerId);
124133
$this->addProductToCompareList->execute($listId, $products, $context);
125134
$this->productCompareHelper->calculate();
135+
$this->compareCookieManager->invalidate();
126136
}
127137
}
128138
} catch (LocalizedException $exception) {

app/code/Magento/CompareListGraphQl/Model/Resolver/RemoveProductsFromCompareList.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2020 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
1011
use Magento\Catalog\Helper\Product\Compare;
1112
use Magento\Catalog\Model\MaskedListIdToCompareListId;
1213
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
@@ -51,26 +52,35 @@ class RemoveProductsFromCompareList implements ResolverInterface
5152
*/
5253
private mixed $productCompareHelper;
5354

55+
/**
56+
* @var CompareCookieManager
57+
*/
58+
private CompareCookieManager $compareCookieManager;
59+
5460
/**
5561
* @param GetCompareList $getCompareList
5662
* @param RemoveFromCompareList $removeFromCompareList
5763
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
5864
* @param GetListIdByCustomerId $getListIdByCustomerId
5965
* @param Compare|null $productCompareHelper
66+
* @param CompareCookieManager|null $compareCookieManager
6067
*/
6168
public function __construct(
6269
GetCompareList $getCompareList,
6370
RemoveFromCompareList $removeFromCompareList,
6471
MaskedListIdToCompareListId $maskedListIdToCompareListId,
6572
GetListIdByCustomerId $getListIdByCustomerId,
66-
?Compare $productCompareHelper = null
73+
?Compare $productCompareHelper = null,
74+
?CompareCookieManager $compareCookieManager = null
6775
) {
6876
$this->getCompareList = $getCompareList;
6977
$this->removeFromCompareList = $removeFromCompareList;
7078
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
7179
$this->getListIdByCustomerId = $getListIdByCustomerId;
7280
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
7381
->get(Compare::class);
82+
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
83+
->get(CompareCookieManager::class);
7484
}
7585

7686
/**
@@ -123,6 +133,7 @@ public function resolve(
123133
try {
124134
$this->removeFromCompareList->execute($listId, $args['input']['products']);
125135
$this->productCompareHelper->calculate();
136+
$this->compareCookieManager->invalidate();
126137
} catch (LocalizedException $exception) {
127138
throw new GraphQlInputException(
128139
__('Something was wrong during removing products from compare list')
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CompareListGraphQl\Model\Service;
9+
10+
use Magento\Framework\Stdlib\CookieManagerInterface;
11+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
12+
use Magento\Framework\Exception\InputException;
13+
use Magento\Framework\Stdlib\Cookie\FailureToSendException;
14+
use Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* Service for managing compare list cookies
19+
*/
20+
class CompareCookieManager
21+
{
22+
/**
23+
* Name of cookie that holds compare products section data
24+
*/
25+
const COOKIE_COMPARE_PRODUCTS = 'section_data_ids';
26+
27+
/**
28+
* Cookie path
29+
*/
30+
const COOKIE_PATH = '/';
31+
32+
/**
33+
* Cookie lifetime value
34+
*/
35+
const COOKIE_LIFETIME = 86400;
36+
37+
/**
38+
* @var CookieManagerInterface
39+
*/
40+
private CookieManagerInterface $cookieManager;
41+
42+
/**
43+
* @var CookieMetadataFactory
44+
*/
45+
private CookieMetadataFactory $cookieMetadataFactory;
46+
47+
/**
48+
* @var LoggerInterface
49+
*/
50+
private LoggerInterface $logger;
51+
52+
/**
53+
* @param CookieManagerInterface $cookieManager
54+
* @param CookieMetadataFactory $cookieMetadataFactory
55+
* @param LoggerInterface $logger
56+
*/
57+
public function __construct(
58+
CookieManagerInterface $cookieManager,
59+
CookieMetadataFactory $cookieMetadataFactory,
60+
LoggerInterface $logger
61+
) {
62+
$this->cookieManager = $cookieManager;
63+
$this->cookieMetadataFactory = $cookieMetadataFactory;
64+
$this->logger = $logger;
65+
}
66+
67+
/**
68+
* Marks compare products section as invalid by updating the cookie value
69+
*
70+
* @return void
71+
*/
72+
public function invalidate(): void
73+
{
74+
try {
75+
$cookieValue = json_encode(['compare-products' => time()]);
76+
$this->setCookie($cookieValue);
77+
} catch (\Exception $e) {
78+
$this->logger->error('Error invalidating compare products cookie: ' . $e->getMessage());
79+
}
80+
}
81+
82+
/**
83+
* Set compare products cookie
84+
*
85+
* @param string $value
86+
* @return void
87+
* @throws InputException
88+
* @throws CookieSizeLimitReachedException
89+
* @throws FailureToSendException
90+
*/
91+
private function setCookie(string $value): void
92+
{
93+
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
94+
->setDuration(self::COOKIE_LIFETIME)
95+
->setPath(self::COOKIE_PATH)
96+
->setHttpOnly(false);
97+
98+
$this->cookieManager->setPublicCookie(
99+
self::COOKIE_COMPARE_PRODUCTS,
100+
$value,
101+
$publicCookieMetadata
102+
);
103+
}
104+
}

0 commit comments

Comments
 (0)