Skip to content

Commit 8aa02bb

Browse files
Merge remote-tracking branch '39967/fix-for-issue-39905' into comprs_oct
2 parents b62122d + b0c0764 commit 8aa02bb

File tree

5 files changed

+398
-4
lines changed

5 files changed

+398
-4
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
11+
use Magento\Catalog\Helper\Product\Compare;
1012
use Magento\Catalog\Model\MaskedListIdToCompareListId;
1113
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
1214
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
1315
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
16+
use Magento\Framework\App\ObjectManager;
1417
use Magento\Framework\Exception\LocalizedException;
1518
use Magento\Framework\GraphQl\Config\Element\Field;
1619
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -21,6 +24,8 @@
2124

2225
/**
2326
* Add products item to compare list
27+
*
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2429
*/
2530
class AddProductsToCompareList implements ResolverInterface
2631
{
@@ -44,22 +49,40 @@ class AddProductsToCompareList implements ResolverInterface
4449
*/
4550
private $getListIdByCustomerId;
4651

52+
/**
53+
* @var Compare
54+
*/
55+
private mixed $productCompareHelper;
56+
57+
/**
58+
* @var CompareCookieManager
59+
*/
60+
private CompareCookieManager $compareCookieManager;
61+
4762
/**
4863
* @param AddToCompareList $addProductToCompareList
4964
* @param GetCompareList $getCompareList
5065
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
5166
* @param GetListIdByCustomerId $getListIdByCustomerId
67+
* @param Compare|null $productCompareHelper
68+
* @param CompareCookieManager|null $compareCookieManager
5269
*/
5370
public function __construct(
5471
AddToCompareList $addProductToCompareList,
5572
GetCompareList $getCompareList,
5673
MaskedListIdToCompareListId $maskedListIdToCompareListId,
57-
GetListIdByCustomerId $getListIdByCustomerId
74+
GetListIdByCustomerId $getListIdByCustomerId,
75+
?Compare $productCompareHelper = null,
76+
?CompareCookieManager $compareCookieManager = null
5877
) {
5978
$this->addProductToCompareList = $addProductToCompareList;
6079
$this->getCompareList = $getCompareList;
6180
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
6281
$this->getListIdByCustomerId = $getListIdByCustomerId;
82+
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
83+
->get(Compare::class);
84+
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
85+
->get(CompareCookieManager::class);
6386
}
6487

6588
/**
@@ -97,13 +120,14 @@ public function resolve(
97120
throw new GraphQlInputException(__($exception->getMessage()));
98121
}
99122

100-
101123
if (!$listId) {
102124
throw new GraphQlInputException(__('"uid" value does not exist'));
103125
}
104126

105127
try {
106128
$this->addProductToCompareList->execute($listId, $args['input']['products'], $context);
129+
$this->productCompareHelper->calculate();
130+
$this->compareCookieManager->invalidate();
107131
} catch (\Exception $exception) {
108132
throw new GraphQlInputException(__($exception->getMessage()));
109133
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
11+
use Magento\Catalog\Helper\Product\Compare;
1012
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
1113
use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService;
1214
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
1315
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
16+
use Magento\Framework\App\ObjectManager;
1417
use Magento\Framework\Exception\LocalizedException;
1518
use Magento\Framework\GraphQl\Config\Element\Field;
1619
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -22,6 +25,8 @@
2225

2326
/**
2427
* Class for creating compare list
28+
*
29+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2530
*/
2631
class CreateCompareList implements ResolverInterface
2732
{
@@ -50,25 +55,43 @@ class CreateCompareList implements ResolverInterface
5055
*/
5156
private $createCompareList;
5257

58+
/**
59+
* @var Compare
60+
*/
61+
private mixed $productCompareHelper;
62+
63+
/**
64+
* @var CompareCookieManager
65+
*/
66+
private CompareCookieManager $compareCookieManager;
67+
5368
/**
5469
* @param Random $mathRandom
5570
* @param GetListIdByCustomerId $getListIdByCustomerId
5671
* @param AddToCompareList $addProductToCompareList
5772
* @param GetCompareList $getCompareList
5873
* @param CreateCompareListService $createCompareList
74+
* @param Compare|null $productCompareHelper
75+
* @param CompareCookieManager|null $compareCookieManager
5976
*/
6077
public function __construct(
6178
Random $mathRandom,
6279
GetListIdByCustomerId $getListIdByCustomerId,
6380
AddToCompareList $addProductToCompareList,
6481
GetCompareList $getCompareList,
65-
CreateCompareListService $createCompareList
82+
CreateCompareListService $createCompareList,
83+
?Compare $productCompareHelper = null,
84+
?CompareCookieManager $compareCookieManager = null
6685
) {
6786
$this->mathRandom = $mathRandom;
6887
$this->getListIdByCustomerId = $getListIdByCustomerId;
6988
$this->addProductToCompareList = $addProductToCompareList;
7089
$this->getCompareList = $getCompareList;
7190
$this->createCompareList = $createCompareList;
91+
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
92+
->get(Compare::class);
93+
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
94+
->get(CompareCookieManager::class);
7295
}
7396

7497
/**
@@ -111,6 +134,8 @@ public function resolve(
111134
} else {
112135
$listId = $this->createCompareList->execute($generatedListId, $customerId);
113136
$this->addProductToCompareList->execute($listId, $products, $context);
137+
$this->productCompareHelper->calculate();
138+
$this->compareCookieManager->invalidate();
114139
}
115140
}
116141
} catch (LocalizedException $exception) {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\CompareListGraphQl\Model\Service\CompareCookieManager;
11+
use Magento\Catalog\Helper\Product\Compare;
1012
use Magento\Catalog\Model\MaskedListIdToCompareListId;
1113
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
1214
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
1315
use Magento\CompareListGraphQl\Model\Service\RemoveFromCompareList;
16+
use Magento\Framework\App\ObjectManager;
1417
use Magento\Framework\Exception\LocalizedException;
1518
use Magento\Framework\GraphQl\Config\Element\Field;
1619
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -21,6 +24,8 @@
2124

2225
/**
2326
* Remove items from compare list
27+
*
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2429
*/
2530
class RemoveProductsFromCompareList implements ResolverInterface
2631
{
@@ -44,22 +49,40 @@ class RemoveProductsFromCompareList implements ResolverInterface
4449
*/
4550
private $getListIdByCustomerId;
4651

52+
/**
53+
* @var Compare
54+
*/
55+
private mixed $productCompareHelper;
56+
57+
/**
58+
* @var CompareCookieManager
59+
*/
60+
private CompareCookieManager $compareCookieManager;
61+
4762
/**
4863
* @param GetCompareList $getCompareList
4964
* @param RemoveFromCompareList $removeFromCompareList
5065
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
5166
* @param GetListIdByCustomerId $getListIdByCustomerId
67+
* @param Compare|null $productCompareHelper
68+
* @param CompareCookieManager|null $compareCookieManager
5269
*/
5370
public function __construct(
5471
GetCompareList $getCompareList,
5572
RemoveFromCompareList $removeFromCompareList,
5673
MaskedListIdToCompareListId $maskedListIdToCompareListId,
57-
GetListIdByCustomerId $getListIdByCustomerId
74+
GetListIdByCustomerId $getListIdByCustomerId,
75+
?Compare $productCompareHelper = null,
76+
?CompareCookieManager $compareCookieManager = null
5877
) {
5978
$this->getCompareList = $getCompareList;
6079
$this->removeFromCompareList = $removeFromCompareList;
6180
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
6281
$this->getListIdByCustomerId = $getListIdByCustomerId;
82+
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
83+
->get(Compare::class);
84+
$this->compareCookieManager = $compareCookieManager ?: ObjectManager::getInstance()
85+
->get(CompareCookieManager::class);
6386
}
6487

6588
/**
@@ -111,6 +134,8 @@ public function resolve(
111134

112135
try {
113136
$this->removeFromCompareList->execute($listId, $args['input']['products']);
137+
$this->productCompareHelper->calculate();
138+
$this->compareCookieManager->invalidate();
114139
} catch (LocalizedException $exception) {
115140
throw new GraphQlInputException(
116141
__('Something was wrong during removing products from compare list')
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 Exception;
11+
use Magento\Framework\Stdlib\CookieManagerInterface;
12+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
13+
use Magento\Framework\Exception\InputException;
14+
use Magento\Framework\Stdlib\Cookie\FailureToSendException;
15+
use Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException;
16+
use Psr\Log\LoggerInterface;
17+
18+
/**
19+
* Service for managing compare list cookies
20+
*
21+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
22+
*/
23+
class CompareCookieManager
24+
{
25+
/**
26+
* Name of cookie that holds compare products section data
27+
*/
28+
public const COOKIE_COMPARE_PRODUCTS = 'section_data_ids';
29+
30+
/**
31+
* The path for which the cookie will be available
32+
*/
33+
public const COOKIE_PATH = '/';
34+
35+
/**
36+
* Cookie lifetime value in seconds (86400 = 24 hours)
37+
*/
38+
public const COOKIE_LIFETIME = 86400;
39+
40+
/**
41+
* @param CookieManagerInterface $cookieManager
42+
* @param CookieMetadataFactory $cookieMetadataFactory
43+
* @param LoggerInterface $logger
44+
*/
45+
public function __construct(
46+
private readonly CookieManagerInterface $cookieManager,
47+
private readonly CookieMetadataFactory $cookieMetadataFactory,
48+
private readonly LoggerInterface $logger
49+
) {
50+
}
51+
52+
/**
53+
* Marks compare products section as invalid by updating the cookie value
54+
*
55+
* @return void
56+
*/
57+
public function invalidate(): void
58+
{
59+
try {
60+
$cookieValue = json_encode(['compare-products' => time()]);
61+
$this->setCookie($cookieValue);
62+
} catch (Exception $e) {
63+
$this->logger->error('Error invalidating compare products cookie: ' . $e->getMessage());
64+
}
65+
}
66+
67+
/**
68+
* Set compare products cookie
69+
*
70+
* @param string $value
71+
* @return void
72+
* @throws InputException
73+
* @throws CookieSizeLimitReachedException
74+
* @throws FailureToSendException
75+
*/
76+
private function setCookie(string $value): void
77+
{
78+
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
79+
->setDuration(self::COOKIE_LIFETIME)
80+
->setPath(self::COOKIE_PATH)
81+
->setHttpOnly(false);
82+
83+
$this->cookieManager->setPublicCookie(
84+
self::COOKIE_COMPARE_PRODUCTS,
85+
$value,
86+
$publicCookieMetadata
87+
);
88+
}
89+
}

0 commit comments

Comments
 (0)