Skip to content

Commit dc7f669

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4244' into PR_2025_11_07_muntianu
2 parents ba9e7d2 + 28e7cf2 commit dc7f669

File tree

4 files changed

+198
-38
lines changed

4 files changed

+198
-38
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,23 @@ public function updateCustomerFromVisitor($object)
259259
return $this;
260260
}
261261

262+
/**
263+
* Assign customer to compare list items
264+
*
265+
* @param int $listId
266+
* @param int $customerId
267+
* @return $this
268+
*/
269+
public function updateCustomerIdForListItems(int $listId, int $customerId)
270+
{
271+
$this->getConnection()->update(
272+
$this->getMainTable(),
273+
['customer_id' => $customerId],
274+
['list_id = ?' => $listId]
275+
);
276+
return $this;
277+
}
278+
262279
/**
263280
* Clear compare items by visitor and/or customer
264281
*
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CompareListGraphQl\Model\Service\Customer;
9+
10+
use Magento\Catalog\Model\CompareList;
11+
use Magento\Catalog\Model\CompareListFactory;
12+
use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList;
13+
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory as CompareItemsCollectionFactory;
14+
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
17+
18+
/**
19+
* Merge guest compare list into customer compare list
20+
*/
21+
class MergeCompareLists
22+
{
23+
/**
24+
* @var CompareItemsCollectionFactory
25+
*/
26+
private $itemCollectionFactory;
27+
28+
/**
29+
* @var AddToCompareList
30+
*/
31+
private $addProductToCompareList;
32+
33+
/**
34+
* @var ResourceCompareList
35+
*/
36+
private $resourceCompareList;
37+
38+
/**
39+
* @var CompareListFactory
40+
*/
41+
private $compareListFactory;
42+
43+
/**
44+
* @param CompareItemsCollectionFactory $itemCollectionFactory
45+
* @param AddToCompareList $addProductToCompareList
46+
* @param ResourceCompareList $resourceCompareList
47+
* @param CompareListFactory $compareListFactory
48+
*/
49+
public function __construct(
50+
CompareItemsCollectionFactory $itemCollectionFactory,
51+
AddToCompareList $addProductToCompareList,
52+
ResourceCompareList $resourceCompareList,
53+
CompareListFactory $compareListFactory
54+
) {
55+
$this->itemCollectionFactory = $itemCollectionFactory;
56+
$this->addProductToCompareList = $addProductToCompareList;
57+
$this->resourceCompareList = $resourceCompareList;
58+
$this->compareListFactory = $compareListFactory;
59+
}
60+
61+
/**
62+
* Merge guest compare list into customer compare list
63+
*
64+
* @param int $guestListId
65+
* @param int $customerListId
66+
* @param ContextInterface $context
67+
* @return CompareList
68+
* @throws LocalizedException
69+
* @throws \Exception
70+
*/
71+
public function execute(int $guestListId, int $customerListId, ContextInterface $context): CompareList
72+
{
73+
if ($guestListId === $customerListId) {
74+
throw new LocalizedException(__('Cannot merge a list with itself.'));
75+
}
76+
77+
$connection = $this->resourceCompareList->getConnection();
78+
$connection->beginTransaction();
79+
80+
try {
81+
$items = $this->itemCollectionFactory->create();
82+
$products = $items->getProductsByListId($guestListId);
83+
84+
$this->addProductToCompareList->execute($customerListId, $products, $context);
85+
86+
$guestList = $this->compareListFactory->create();
87+
$this->resourceCompareList->load($guestList, $guestListId, 'list_id');
88+
$this->resourceCompareList->delete($guestList);
89+
90+
$connection->commit();
91+
} catch (\Exception $e) {
92+
$connection->rollBack();
93+
throw $e;
94+
}
95+
96+
$customerList = $this->compareListFactory->create();
97+
$this->resourceCompareList->load($customerList, $customerListId, 'list_id');
98+
99+
return $customerList;
100+
}
101+
}

app/code/Magento/CompareListGraphQl/Model/Service/Customer/SetCustomerToCompareList.php

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
use Magento\Catalog\Model\CompareList;
1111
use Magento\Catalog\Model\CompareListFactory;
1212
use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList;
13-
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection;
14-
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory as CompareItemsCollectionFactory;
15-
use Magento\CompareListGraphQl\Model\Service\AddToCompareList;
13+
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item as ResourceCompareItem;
14+
use Magento\CompareListGraphQl\Model\Service\Customer\GetListIdByCustomerId;
15+
use Magento\CompareListGraphQl\Model\Service\Customer\MergeCompareLists;
16+
use Magento\CompareListGraphQl\Model\Service\Customer\ValidateCustomer;
1617
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1718
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1819
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -44,46 +45,45 @@ class SetCustomerToCompareList
4445
private $getListIdByCustomerId;
4546

4647
/**
47-
* @var Collection
48+
* @var ResourceCompareItem
4849
*/
49-
private $items;
50+
private $resourceCompareItem;
5051

5152
/**
52-
* @var CompareItemsCollectionFactory
53+
* @var MergeCompareLists
5354
*/
54-
private $itemCollectionFactory;
55-
56-
/**
57-
* @var AddToCompareList
58-
*/
59-
private $addProductToCompareList;
55+
private $mergeCompareLists;
6056

6157
/**
6258
* @param ValidateCustomer $validateCustomer
6359
* @param CompareListFactory $compareListFactory
6460
* @param ResourceCompareList $resourceCompareList
61+
* @param GetListIdByCustomerId $getListIdByCustomerId
62+
* @param ResourceCompareItem $resourceCompareItem
63+
* @param MergeCompareLists $mergeCompareLists
6564
*/
6665
public function __construct(
6766
ValidateCustomer $validateCustomer,
6867
CompareListFactory $compareListFactory,
6968
ResourceCompareList $resourceCompareList,
7069
GetListIdByCustomerId $getListIdByCustomerId,
71-
CompareItemsCollectionFactory $itemCollectionFactory,
72-
AddToCompareList $addProductToCompareList
70+
ResourceCompareItem $resourceCompareItem,
71+
MergeCompareLists $mergeCompareLists
7372
) {
7473
$this->validateCustomer = $validateCustomer;
7574
$this->compareListFactory = $compareListFactory;
7675
$this->resourceCompareList = $resourceCompareList;
7776
$this->getListIdByCustomerId = $getListIdByCustomerId;
78-
$this->itemCollectionFactory = $itemCollectionFactory;
79-
$this->addProductToCompareList = $addProductToCompareList;
77+
$this->resourceCompareItem = $resourceCompareItem;
78+
$this->mergeCompareLists = $mergeCompareLists;
8079
}
8180

8281
/**
8382
* Set customer to compare list
8483
*
8584
* @param int $listId
8685
* @param int $customerId
86+
* @param ContextInterface $context
8787
*
8888
* @return CompareList
8989
*
@@ -94,21 +94,29 @@ public function __construct(
9494
public function execute(int $listId, int $customerId, ContextInterface $context): ?CompareList
9595
{
9696
if ($this->validateCustomer->execute($customerId)) {
97-
/** @var CompareList $compareListModel */
9897
$compareList = $this->compareListFactory->create();
9998
$customerListId = $this->getListIdByCustomerId->execute($customerId);
10099
$this->resourceCompareList->load($compareList, $listId, 'list_id');
100+
if (!$compareList->getListId()) {
101+
throw new GraphQlNoSuchEntityException(
102+
__('The compare list with ID "%list_id" does not exist.', ['list_id' => $listId])
103+
);
104+
}
101105
if ($customerListId) {
102-
$this->items = $this->itemCollectionFactory->create();
103-
$products = $this->items->getProductsByListId($listId);
104-
$this->addProductToCompareList->execute($customerListId, $products, $context);
105-
$this->resourceCompareList->delete($compareList);
106-
$compareList = $this->compareListFactory->create();
107-
$this->resourceCompareList->load($compareList, $customerListId, 'list_id');
108-
return $compareList;
106+
return $this->mergeCompareLists->execute($listId, $customerListId, $context);
109107
}
110-
$compareList->setCustomerId($customerId);
111-
$this->resourceCompareList->save($compareList);
108+
109+
$this->resourceCompareList->beginTransaction();
110+
try {
111+
$compareList->setCustomerId($customerId);
112+
$this->resourceCompareList->save($compareList);
113+
$this->resourceCompareItem->updateCustomerIdForListItems($listId, $customerId);
114+
$this->resourceCompareList->commit();
115+
} catch (\Exception $e) {
116+
$this->resourceCompareList->rollBack();
117+
throw $e;
118+
}
119+
112120
return $compareList;
113121
}
114122

dev/tests/api-functional/testsuite/Magento/GraphQl/CompareList/CompareListTest.php

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

@@ -84,13 +84,13 @@ public function testAddProductToCompareList()
8484
{
8585
$compareList = $this->createCompareList();
8686
$uid = $compareList['createCompareList']['uid'];
87-
$this->assertEquals(0, $compareList['createCompareList']['item_count'],'Incorrect count');
87+
$this->assertEquals(0, $compareList['createCompareList']['item_count'], 'Incorrect count');
8888
$this->uidAssertion($uid);
8989
$response = $this->addProductsToCompareList($uid);
9090
$resultUid = $response['addProductsToCompareList']['uid'];
9191
$this->uidAssertion($resultUid);
9292
$this->itemsAssertion($response['addProductsToCompareList']['items']);
93-
$this->assertEquals(2, $response['addProductsToCompareList']['item_count'],'Incorrect count');
93+
$this->assertEquals(2, $response['addProductsToCompareList']['item_count'], 'Incorrect count');
9494
$this->assertResponseFields(
9595
$response['addProductsToCompareList']['attributes'],
9696
[
@@ -194,20 +194,24 @@ public function testDeleteCompareList()
194194
}
195195

196196
/**
197-
* Assign compare list to customer
197+
* Assign compare list to customer and verify products can be removed
198198
*
199199
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
200200
* @magentoApiDataFixture Magento/Customer/_files/customer.php
201201
*/
202202
public function testAssignCompareListToCustomer()
203203
{
204+
$currentEmail = '[email protected]';
205+
$currentPassword = 'password';
206+
204207
$compareList = $this->createCompareList();
205208
$uid = $compareList['createCompareList']['uid'];
206209
$this->uidAssertion($uid);
210+
207211
$addProducts = $this->addProductsToCompareList($uid);
208212
$this->itemsAssertion($addProducts['addProductsToCompareList']['items']);
209-
$currentEmail = '[email protected]';
210-
$currentPassword = 'password';
213+
$this->assertEquals(2, $addProducts['addProductsToCompareList']['item_count']);
214+
211215
$customerQuery = <<<QUERY
212216
{
213217
customer {
@@ -239,6 +243,7 @@ public function testAssignCompareListToCustomer()
239243
result
240244
compare_list {
241245
uid
246+
item_count
242247
items {
243248
uid
244249
}
@@ -253,17 +258,45 @@ public function testAssignCompareListToCustomer()
253258
$this->getCustomerAuthHeaders($currentEmail, $currentPassword)
254259
);
255260
$this->assertTrue($assignResponse['assignCompareListToCustomer']['result']);
261+
$this->assertEquals(2, $assignResponse['assignCompareListToCustomer']['compare_list']['item_count']);
256262

257263
$customerAssignedResponse = $this->graphQlQuery(
258264
$customerQuery,
259265
[],
260266
'',
261267
$this->getCustomerAuthHeaders($currentEmail, $currentPassword)
262268
);
263-
264269
$this->assertArrayHasKey('compare_list', $customerAssignedResponse['customer']);
265270
$this->uidAssertion($customerAssignedResponse['customer']['compare_list']['uid']);
266271
$this->itemsAssertion($customerAssignedResponse['customer']['compare_list']['items']);
272+
273+
$product = $this->productRepository->get(self::PRODUCT_SKU_1);
274+
$removeFromCompareList = <<<MUTATION
275+
mutation{
276+
removeProductsFromCompareList(input: {uid: "{$uid}", products: [{$product->getId()}]}) {
277+
uid
278+
item_count
279+
items {
280+
product {
281+
sku
282+
}
283+
}
284+
}
285+
}
286+
MUTATION;
287+
$removeResponse = $this->graphQlMutation(
288+
$removeFromCompareList,
289+
[],
290+
'',
291+
$this->getCustomerAuthHeaders($currentEmail, $currentPassword)
292+
);
293+
294+
$this->assertEquals(1, $removeResponse['removeProductsFromCompareList']['item_count']);
295+
$this->assertCount(1, $removeResponse['removeProductsFromCompareList']['items']);
296+
$this->assertEquals(
297+
self::PRODUCT_SKU_2,
298+
$removeResponse['removeProductsFromCompareList']['items'][0]['product']['sku']
299+
);
267300
}
268301

269302
/**
@@ -290,7 +323,8 @@ public function testCompareListsNotAccessibleBetweenCustomers()
290323
}
291324
MUTATION;
292325

293-
$expectedExceptionsMessage = 'GraphQL response contains errors: This customer is not authorized to access this list';
326+
$expectedExceptionsMessage = 'GraphQL response contains errors: ' .
327+
'This customer is not authorized to access this list';
294328
$this->expectException(ResponseContainsErrorsException::class);
295329
$this->expectExceptionMessage($expectedExceptionsMessage);
296330
//customer2 not allowed to assign compareList belonging to customer1
@@ -308,17 +342,17 @@ public function testCompareListsNotAccessibleBetweenCustomers()
308342
}
309343
}
310344
MUTATION;
311-
$expectedExceptionsMessage = 'GraphQL response contains errors: This customer is not authorized to access this list';
345+
$expectedExceptionsMessage = 'GraphQL response contains errors: ' .
346+
'This customer is not authorized to access this list';
312347
$this->expectException(ResponseContainsErrorsException::class);
313348
$this->expectExceptionMessage($expectedExceptionsMessage);
314349
//customer1 not allowed to delete compareList belonging to customer2
315350
$this->graphQlMutation(
316-
$assignCompareListToCustomer,
351+
$deleteCompareList,
317352
[],
318353
'',
319354
$this->getCustomerAuthHeaders('[email protected]', 'password')
320355
);
321-
322356
}
323357

324358
/**

0 commit comments

Comments
 (0)