Skip to content

Commit 113ec4a

Browse files
committed
Merge remote-tracking branch 'origin/2.2.10-develop' into MC-18100
2 parents ef5c8ed + 143f7e7 commit 113ec4a

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66
namespace Magento\Catalog\Model\Product\Compare;
77

8+
use Magento\Catalog\Model\ProductRepository;
89
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\NoSuchEntityException;
912

1013
/**
1114
* Product Compare List Model
@@ -51,6 +54,11 @@ class ListCompare extends \Magento\Framework\DataObject
5154
*/
5255
protected $_compareItemFactory;
5356

57+
/**
58+
* @var ProductRepository
59+
*/
60+
private $productRepository;
61+
5462
/**
5563
* Constructor
5664
*
@@ -60,20 +68,23 @@ class ListCompare extends \Magento\Framework\DataObject
6068
* @param \Magento\Customer\Model\Session $customerSession
6169
* @param \Magento\Customer\Model\Visitor $customerVisitor
6270
* @param array $data
71+
* @param ProductRepository|null $productRepository
6372
*/
6473
public function __construct(
6574
\Magento\Catalog\Model\Product\Compare\ItemFactory $compareItemFactory,
6675
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory $itemCollectionFactory,
6776
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem,
6877
\Magento\Customer\Model\Session $customerSession,
6978
\Magento\Customer\Model\Visitor $customerVisitor,
70-
array $data = []
79+
array $data = [],
80+
ProductRepository $productRepository = null
7181
) {
7282
$this->_compareItemFactory = $compareItemFactory;
7383
$this->_itemCollectionFactory = $itemCollectionFactory;
7484
$this->_catalogProductCompareItem = $catalogProductCompareItem;
7585
$this->_customerSession = $customerSession;
7686
$this->_customerVisitor = $customerVisitor;
87+
$this->productRepository = $productRepository ?: ObjectManager::getInstance()->create(ProductRepository::class);
7788
parent::__construct($data);
7889
}
7990

@@ -82,6 +93,7 @@ public function __construct(
8293
*
8394
* @param int|\Magento\Catalog\Model\Product $product
8495
* @return $this
96+
* @throws \Exception
8597
*/
8698
public function addProduct($product)
8799
{
@@ -90,14 +102,33 @@ public function addProduct($product)
90102
$this->_addVisitorToItem($item);
91103
$item->loadByProduct($product);
92104

93-
if (!$item->getId()) {
105+
if (!$item->getId() && $this->productExists($product)) {
94106
$item->addProductData($product);
95107
$item->save();
96108
}
97109

98110
return $this;
99111
}
100112

113+
/**
114+
* Check product exists.
115+
*
116+
* @param int|\Magento\Catalog\Model\Product $product
117+
* @return bool
118+
*/
119+
private function productExists($product)
120+
{
121+
if ($product instanceof \Magento\Catalog\Model\Product && $product->getId()) {
122+
return true;
123+
}
124+
try {
125+
$product = $this->productRepository->getById((int)$product);
126+
return !empty($product->getId());
127+
} catch (NoSuchEntityException $e) {
128+
return false;
129+
}
130+
}
131+
101132
/**
102133
* Add products to compare list
103134
*

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Compare/ListCompareTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
namespace Magento\Catalog\Model\Product\Compare;
88

9-
/**
10-
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
11-
* @magentoDataFixture Magento/Customer/_files/customer.php
12-
*/
139
class ListCompareTest extends \PHPUnit\Framework\TestCase
1410
{
1511
/**
@@ -44,17 +40,44 @@ protected function tearDown()
4440
$this->_session->setCustomerId(null);
4541
}
4642

43+
/**
44+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
45+
* @magentoDataFixture Magento/Customer/_files/customer.php
46+
*/
4747
public function testAddProductWithSession()
4848
{
4949
$this->_session->setCustomerId(1);
5050
/** @var $product \Magento\Catalog\Model\Product */
5151
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
5252
->create(\Magento\Catalog\Model\Product::class)
5353
->load(1);
54-
$this->_model->addProduct($product);
54+
/** @var $product2 \Magento\Catalog\Model\Product */
55+
$product2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
56+
->create(\Magento\Catalog\Model\Product::class)
57+
->load(6);
58+
$products = [$product->getId(), $product2->getId()];
59+
$this->_model->addProducts($products);
60+
5561
$this->assertTrue($this->_model->hasItems(1, $this->_visitor->getId()));
5662
}
5763

64+
/**
65+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
66+
* @magentoDataFixture Magento/Customer/_files/customer.php
67+
*/
68+
public function testAddProductWithSessionNeg()
69+
{
70+
$this->_session->setCustomerId(1);
71+
$products = ['none', 99];
72+
$this->_model->addProducts($products);
73+
74+
$this->assertFalse($this->_model->hasItems(1, $this->_visitor->getId()));
75+
}
76+
77+
/**
78+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
79+
* @magentoDataFixture Magento/Customer/_files/customer.php
80+
*/
5881
public function testAddProductWithoutSession()
5982
{
6083
/** @var $product \Magento\Catalog\Model\Product */

0 commit comments

Comments
 (0)