Skip to content

Commit 5f7ad0e

Browse files
author
Prabhu Ram
committed
Merge commit '27a592cf38b501bcf05d9ef2e5401a9cff7e986b' into compare-products
2 parents d7e1568 + 27a592c commit 5f7ad0e

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public function resolve(
8989

9090
if ($listId) {
9191
try {
92-
$result = $this->setCustomerToCompareList->execute($listId, $context->getUserId());
92+
$result = $this->setCustomerToCompareList->execute($listId, $context->getUserId(), $context);
9393
if ($result) {
9494
return [
9595
'result' => true,
96-
'compare_list' => $this->getCompareList->execute($listId, $context)
96+
'compare_list' => $this->getCompareList->execute((int)$result->getListId(), $context)
9797
];
9898
}
9999
} catch (LocalizedException $exception) {

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
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;
1316
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1417
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1518
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
19+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1620

1721
/**
1822
* Assign customer to compare list
@@ -34,6 +38,26 @@ class SetCustomerToCompareList
3438
*/
3539
private $resourceCompareList;
3640

41+
/**
42+
* @var GetListIdByCustomerId
43+
*/
44+
private $getListIdByCustomerId;
45+
46+
/**
47+
* @var Collection
48+
*/
49+
private $items;
50+
51+
/**
52+
* @var CompareItemsCollectionFactory
53+
*/
54+
private $itemCollectionFactory;
55+
56+
/**
57+
* @var AddToCompareList
58+
*/
59+
private $addProductToCompareList;
60+
3761
/**
3862
* @param ValidateCustomer $validateCustomer
3963
* @param CompareListFactory $compareListFactory
@@ -42,11 +66,17 @@ class SetCustomerToCompareList
4266
public function __construct(
4367
ValidateCustomer $validateCustomer,
4468
CompareListFactory $compareListFactory,
45-
ResourceCompareList $resourceCompareList
69+
ResourceCompareList $resourceCompareList,
70+
GetListIdByCustomerId $getListIdByCustomerId,
71+
CompareItemsCollectionFactory $itemCollectionFactory,
72+
AddToCompareList $addProductToCompareList
4673
) {
4774
$this->validateCustomer = $validateCustomer;
4875
$this->compareListFactory = $compareListFactory;
4976
$this->resourceCompareList = $resourceCompareList;
77+
$this->getListIdByCustomerId = $getListIdByCustomerId;
78+
$this->itemCollectionFactory = $itemCollectionFactory;
79+
$this->addProductToCompareList = $addProductToCompareList;
5080
}
5181

5282
/**
@@ -61,12 +91,22 @@ public function __construct(
6191
* @throws GraphQlInputException
6292
* @throws GraphQlNoSuchEntityException
6393
*/
64-
public function execute(int $listId, int $customerId): ?CompareList
94+
public function execute(int $listId, int $customerId, ContextInterface $context): ?CompareList
6595
{
6696
if ($this->validateCustomer->execute($customerId)) {
6797
/** @var CompareList $compareListModel */
6898
$compareList = $this->compareListFactory->create();
99+
$customerListId = $this->getListIdByCustomerId->execute($customerId);
69100
$this->resourceCompareList->load($compareList, $listId, 'list_id');
101+
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;
109+
}
70110
$compareList->setCustomerId($customerId);
71111
$this->resourceCompareList->save($compareList);
72112
return $compareList;

0 commit comments

Comments
 (0)