Skip to content

Commit 09eb72d

Browse files
#39905 Product Removed on Mobile Still Appears in Web's Mini Compare Section Until Re-login
Integrates the Product Compare helper across compare list resolvers to trigger updates after adding, removing, or creating compare lists. Also forces a reload of compare product data on the frontend to maintain accuracy.
1 parent 4f82be7 commit 09eb72d

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ 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+
4953
initSidebar();
5054
}
5155
});

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

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

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\Catalog\Helper\Product\Compare;
1011
use Magento\Catalog\Model\MaskedListIdToCompareListId;
1112
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
1213
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
1314
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
15+
use Magento\Framework\App\ObjectManager;
1416
use Magento\Framework\Exception\LocalizedException;
1517
use Magento\Framework\GraphQl\Config\Element\Field;
1618
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -44,22 +46,31 @@ class AddProductsToCompareList implements ResolverInterface
4446
*/
4547
private $getListIdByCustomerId;
4648

49+
/**
50+
* @var Compare
51+
*/
52+
private mixed $productCompareHelper;
53+
4754
/**
4855
* @param AddToCompareList $addProductToCompareList
4956
* @param GetCompareList $getCompareList
5057
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
5158
* @param GetListIdByCustomerId $getListIdByCustomerId
59+
* @param Compare|null $productCompareHelper
5260
*/
5361
public function __construct(
5462
AddToCompareList $addProductToCompareList,
5563
GetCompareList $getCompareList,
5664
MaskedListIdToCompareListId $maskedListIdToCompareListId,
57-
GetListIdByCustomerId $getListIdByCustomerId
65+
GetListIdByCustomerId $getListIdByCustomerId,
66+
?Compare $productCompareHelper = null
5867
) {
5968
$this->addProductToCompareList = $addProductToCompareList;
6069
$this->getCompareList = $getCompareList;
6170
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
6271
$this->getListIdByCustomerId = $getListIdByCustomerId;
72+
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
73+
->get(Compare::class);
6374
}
6475

6576
/**
@@ -104,6 +115,7 @@ public function resolve(
104115

105116
try {
106117
$this->addProductToCompareList->execute($listId, $args['input']['products'], $context);
118+
$this->productCompareHelper->calculate();
107119
} catch (\Exception $exception) {
108120
throw new GraphQlInputException(__($exception->getMessage()));
109121
}

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

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

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\Catalog\Helper\Product\Compare;
1011
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
1112
use Magento\CompareListGraphQl\Model\Service\CreateCompareList as CreateCompareListService;
1213
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
1314
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
15+
use Magento\Framework\App\ObjectManager;
1416
use Magento\Framework\Exception\LocalizedException;
1517
use Magento\Framework\GraphQl\Config\Element\Field;
1618
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -50,25 +52,34 @@ class CreateCompareList implements ResolverInterface
5052
*/
5153
private $createCompareList;
5254

55+
/**
56+
* @var Compare
57+
*/
58+
private mixed $productCompareHelper;
59+
5360
/**
5461
* @param Random $mathRandom
5562
* @param GetListIdByCustomerId $getListIdByCustomerId
5663
* @param AddToCompareList $addProductToCompareList
5764
* @param GetCompareList $getCompareList
5865
* @param CreateCompareListService $createCompareList
66+
* @param Compare|null $productCompareHelper
5967
*/
6068
public function __construct(
6169
Random $mathRandom,
6270
GetListIdByCustomerId $getListIdByCustomerId,
6371
AddToCompareList $addProductToCompareList,
6472
GetCompareList $getCompareList,
65-
CreateCompareListService $createCompareList
73+
CreateCompareListService $createCompareList,
74+
?Compare $productCompareHelper = null
6675
) {
6776
$this->mathRandom = $mathRandom;
6877
$this->getListIdByCustomerId = $getListIdByCustomerId;
6978
$this->addProductToCompareList = $addProductToCompareList;
7079
$this->getCompareList = $getCompareList;
7180
$this->createCompareList = $createCompareList;
81+
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
82+
->get(Compare::class);
7283
}
7384

7485
/**
@@ -111,6 +122,7 @@ public function resolve(
111122
} else {
112123
$listId = $this->createCompareList->execute($generatedListId, $customerId);
113124
$this->addProductToCompareList->execute($listId, $products, $context);
125+
$this->productCompareHelper->calculate();
114126
}
115127
}
116128
} catch (LocalizedException $exception) {

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

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

88
namespace Magento\CompareListGraphQl\Model\Resolver;
99

10+
use Magento\Catalog\Helper\Product\Compare;
1011
use Magento\Catalog\Model\MaskedListIdToCompareListId;
1112
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
1213
use Magento\CompareListGraphQl\Model\Service\GetCompareList;
1314
use Magento\CompareListGraphQl\Model\Service\RemoveFromCompareList;
15+
use Magento\Framework\App\ObjectManager;
1416
use Magento\Framework\Exception\LocalizedException;
1517
use Magento\Framework\GraphQl\Config\Element\Field;
1618
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -44,22 +46,31 @@ class RemoveProductsFromCompareList implements ResolverInterface
4446
*/
4547
private $getListIdByCustomerId;
4648

49+
/**
50+
* @var Compare
51+
*/
52+
private mixed $productCompareHelper;
53+
4754
/**
4855
* @param GetCompareList $getCompareList
4956
* @param RemoveFromCompareList $removeFromCompareList
5057
* @param MaskedListIdToCompareListId $maskedListIdToCompareListId
5158
* @param GetListIdByCustomerId $getListIdByCustomerId
59+
* @param Compare|null $productCompareHelper
5260
*/
5361
public function __construct(
5462
GetCompareList $getCompareList,
5563
RemoveFromCompareList $removeFromCompareList,
5664
MaskedListIdToCompareListId $maskedListIdToCompareListId,
57-
GetListIdByCustomerId $getListIdByCustomerId
65+
GetListIdByCustomerId $getListIdByCustomerId,
66+
?Compare $productCompareHelper = null
5867
) {
5968
$this->getCompareList = $getCompareList;
6069
$this->removeFromCompareList = $removeFromCompareList;
6170
$this->maskedListIdToCompareListId = $maskedListIdToCompareListId;
6271
$this->getListIdByCustomerId = $getListIdByCustomerId;
72+
$this->productCompareHelper = $productCompareHelper ?: ObjectManager::getInstance()
73+
->get(Compare::class);
6374
}
6475

6576
/**
@@ -111,6 +122,7 @@ public function resolve(
111122

112123
try {
113124
$this->removeFromCompareList->execute($listId, $args['input']['products']);
125+
$this->productCompareHelper->calculate();
114126
} catch (LocalizedException $exception) {
115127
throw new GraphQlInputException(
116128
__('Something was wrong during removing products from compare list')

0 commit comments

Comments
 (0)