5
5
*/
6
6
namespace Magento \Catalog \Model \Product \Compare ;
7
7
8
+ use Magento \Catalog \Model \ProductRepository ;
8
9
use Magento \Catalog \Model \ResourceModel \Product \Compare \Item \Collection ;
10
+ use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Framework \Exception \NoSuchEntityException ;
9
12
10
13
/**
11
14
* Product Compare List Model
@@ -51,6 +54,11 @@ class ListCompare extends \Magento\Framework\DataObject
51
54
*/
52
55
protected $ _compareItemFactory ;
53
56
57
+ /**
58
+ * @var ProductRepository
59
+ */
60
+ private $ productRepository ;
61
+
54
62
/**
55
63
* Constructor
56
64
*
@@ -60,20 +68,23 @@ class ListCompare extends \Magento\Framework\DataObject
60
68
* @param \Magento\Customer\Model\Session $customerSession
61
69
* @param \Magento\Customer\Model\Visitor $customerVisitor
62
70
* @param array $data
71
+ * @param ProductRepository|null $productRepository
63
72
*/
64
73
public function __construct (
65
74
\Magento \Catalog \Model \Product \Compare \ItemFactory $ compareItemFactory ,
66
75
\Magento \Catalog \Model \ResourceModel \Product \Compare \Item \CollectionFactory $ itemCollectionFactory ,
67
76
\Magento \Catalog \Model \ResourceModel \Product \Compare \Item $ catalogProductCompareItem ,
68
77
\Magento \Customer \Model \Session $ customerSession ,
69
78
\Magento \Customer \Model \Visitor $ customerVisitor ,
70
- array $ data = []
79
+ array $ data = [],
80
+ ProductRepository $ productRepository = null
71
81
) {
72
82
$ this ->_compareItemFactory = $ compareItemFactory ;
73
83
$ this ->_itemCollectionFactory = $ itemCollectionFactory ;
74
84
$ this ->_catalogProductCompareItem = $ catalogProductCompareItem ;
75
85
$ this ->_customerSession = $ customerSession ;
76
86
$ this ->_customerVisitor = $ customerVisitor ;
87
+ $ this ->productRepository = $ productRepository ?: ObjectManager::getInstance ()->create (ProductRepository::class);
77
88
parent ::__construct ($ data );
78
89
}
79
90
@@ -82,6 +93,7 @@ public function __construct(
82
93
*
83
94
* @param int|\Magento\Catalog\Model\Product $product
84
95
* @return $this
96
+ * @throws \Exception
85
97
*/
86
98
public function addProduct ($ product )
87
99
{
@@ -90,14 +102,33 @@ public function addProduct($product)
90
102
$ this ->_addVisitorToItem ($ item );
91
103
$ item ->loadByProduct ($ product );
92
104
93
- if (!$ item ->getId ()) {
105
+ if (!$ item ->getId () && $ this -> productExists ( $ product ) ) {
94
106
$ item ->addProductData ($ product );
95
107
$ item ->save ();
96
108
}
97
109
98
110
return $ this ;
99
111
}
100
112
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
+
101
132
/**
102
133
* Add products to compare list
103
134
*
0 commit comments