1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Richdynamix\PersonalisedProducts\Model\Frontend\Checkout\Cart; |
4
|
|
|
|
5
|
|
|
use Magento\Catalog\Model\Product\Visibility; |
6
|
|
|
use \Magento\Catalog\Model\ProductFactory as ProductFactory; |
7
|
|
|
use \Richdynamix\PersonalisedProducts\Model\PredictionIO\EngineClient\Complementary; |
8
|
|
|
use \Magento\Customer\Model\Session as CustomerSession; |
9
|
|
|
use \Magento\Checkout\Model\Session; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Crosssell |
13
|
|
|
* |
14
|
|
|
* @category Richdynamix |
15
|
|
|
* @package PersonalisedProducts |
16
|
|
|
* @author Steven Richardson ([email protected]) @mage_gizmo |
17
|
|
|
*/ |
18
|
|
|
class Crosssell |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Complementary |
22
|
|
|
*/ |
23
|
|
|
private $_complementaryEngine; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CustomerSession |
27
|
|
|
*/ |
28
|
|
|
private $_customerSession; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $_basketProducts; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ProductFactory |
37
|
|
|
*/ |
38
|
|
|
private $_productFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $_products; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Session |
47
|
|
|
*/ |
48
|
|
|
protected $_checkoutSession; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Crosssell constructor. |
52
|
|
|
* @param Complementary $complementaryEngine |
53
|
|
|
* @param CustomerSession $customerSession |
54
|
|
|
* @param ProductFactory $productFactory |
55
|
|
|
* @param Session $session |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
Complementary $complementaryEngine, |
59
|
|
|
CustomerSession $customerSession, |
60
|
|
|
ProductFactory $productFactory, |
61
|
|
|
Session $session |
62
|
|
|
) { |
63
|
|
|
$this->_complementaryEngine = $complementaryEngine; |
64
|
|
|
$this->_customerSession = $customerSession; |
65
|
|
|
$this->_productFactory = $productFactory; |
66
|
|
|
$this->_checkoutSession = $session; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Query the PredictionIO engine for product data |
71
|
|
|
* |
72
|
|
|
* @return array|bool |
73
|
|
|
*/ |
74
|
|
|
public function getProductCollection() |
75
|
|
|
{ |
76
|
|
|
$this->_basketProducts = $this->_getCartProductIds(); |
77
|
|
|
$products = $this->_complementaryEngine->sendQuery($this->_basketProducts); |
78
|
|
|
|
79
|
|
|
if ($products['rules']) { |
80
|
|
|
return $this->_getPredictedProducts($products['rules']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Loop over each of the rules in the returned data from PredictionIO |
88
|
|
|
* |
89
|
|
|
* @param array $items |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
private function _getPredictedProducts(array $items) |
93
|
|
|
{ |
94
|
|
|
$productIds = []; |
95
|
|
|
foreach ($items as $item) { |
96
|
|
|
$this->_getProductIds($item['itemScores'], $productIds); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $productIds; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Build product ID collection array from PredictionIO engine data |
104
|
|
|
* |
105
|
|
|
* @param array $items |
106
|
|
|
* @param $productIds |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
private function _getProductIds(array $items, &$productIds) |
110
|
|
|
{ |
111
|
|
|
foreach ($items as $item) { |
112
|
|
|
if (!in_array($item['item'], $this->_basketProducts)) { |
113
|
|
|
$productIds[] = $item['item']; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get a new product collection from prediction IO result set |
122
|
|
|
* |
123
|
|
|
* @param array $personalisedIds |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function getPersonalisedProductCollection(array $personalisedIds) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$collection = $this->_productFactory->create()->getCollection() |
129
|
|
|
->addAttributeToFilter('entity_id', ['in', $personalisedIds]) |
130
|
|
|
->addAttributeToFilter('visibility', Visibility::VISIBILITY_BOTH) |
131
|
|
|
->addAttributeToFilter('status', array('eq' => 1)); |
132
|
|
|
return $collection; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get all product ids in the cart |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
private function _getCartProductIds() |
141
|
|
|
{ |
142
|
|
|
if ($this->_products === null) { |
143
|
|
|
$this->_products = []; |
144
|
|
|
foreach ($this->getQuote()->getAllItems() as $quoteItem) { |
145
|
|
|
/* @var $quoteItem \Magento\Quote\Model\Quote\Item */ |
146
|
|
|
$product = $quoteItem->getProduct(); |
147
|
|
|
$this->_products[] = $product->getEntityId(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->_products; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get the quote from the session |
156
|
|
|
* |
157
|
|
|
* @return \Magento\Quote\Model\Quote |
158
|
|
|
*/ |
159
|
|
|
public function getQuote() |
160
|
|
|
{ |
161
|
|
|
return $this->_checkoutSession->getQuote(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.