1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Richdynamix\PersonalisedProducts\Block\Product\ProductList; |
4
|
|
|
|
5
|
|
|
use \Magento\Framework\View\Element\Template; |
6
|
|
|
use \Magento\Catalog\Block\Product\Context as Context; |
7
|
|
|
use \Magento\Checkout\Model\ResourceModel\Cart as Cart; |
8
|
|
|
use \Magento\Catalog\Model\Product\Visibility as Visibility; |
9
|
|
|
use \Magento\Checkout\Model\Session as Session; |
10
|
|
|
use \Magento\Framework\Module\Manager as Manager; |
11
|
|
|
use Richdynamix\PersonalisedProducts\Helper\Config as Config; |
12
|
|
|
use \Magento\Catalog\Model\ProductFactory as ProductFactory; |
13
|
|
|
use \Magento\Customer\Model\Session as CustomerSession; |
14
|
|
|
use \Richdynamix\PersonalisedProducts\Model\Frontend\Catalog\Product\ProductList\Upsell as PersonalisedUpsell; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Rewrite product upsell block to switch out product collection |
18
|
|
|
* for one returned from PredictionIO |
19
|
|
|
* |
20
|
|
|
* @category Richdynamix |
21
|
|
|
* @package PersonalisedProducts |
22
|
|
|
* @author Steven Richardson ([email protected]) @mage_gizmo |
23
|
|
|
*/ |
24
|
|
|
class Upsell extends \Magento\Catalog\Block\Product\ProductList\Upsell |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Config |
28
|
|
|
*/ |
29
|
|
|
private $_config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ProductFactory |
33
|
|
|
*/ |
34
|
|
|
private $_productFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var PersonalisedUpsell |
38
|
|
|
*/ |
39
|
|
|
private $_upsell; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $_isPredictedResults = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Upsell constructor. |
48
|
|
|
* @param Context $context |
49
|
|
|
* @param Cart $checkoutCart |
50
|
|
|
* @param Visibility $productVisibility |
51
|
|
|
* @param Session $checkoutSession |
52
|
|
|
* @param Manager $moduleManager |
53
|
|
|
* @param ProductFactory $productFactory |
54
|
|
|
* @param Config $config |
55
|
|
|
* @param PersonalisedUpsell $upsell |
56
|
|
|
* @param CustomerSession $customerSession |
57
|
|
|
* @param array $data |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
60
|
|
|
Context $context, |
61
|
|
|
Cart $checkoutCart, |
62
|
|
|
Visibility $productVisibility, |
63
|
|
|
Session $checkoutSession, |
64
|
|
|
Manager $moduleManager, |
65
|
|
|
ProductFactory $productFactory, |
66
|
|
|
Config $config, |
67
|
|
|
PersonalisedUpsell $upsell, |
68
|
|
|
CustomerSession $customerSession, |
69
|
|
|
array $data = [] |
70
|
|
|
) { |
71
|
|
|
$this->_config = $config; |
72
|
|
|
$this->_productFactory = $productFactory; |
73
|
|
|
$this->_upsell = $upsell; |
74
|
|
|
$this->_customerSession = $customerSession; |
75
|
|
|
parent::__construct( |
76
|
|
|
$context, |
77
|
|
|
$checkoutCart, |
78
|
|
|
$productVisibility, |
79
|
|
|
$checkoutSession, |
80
|
|
|
$moduleManager, |
81
|
|
|
$data |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Rewrite parent _prepareData method to use PredictionIO results when available |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
protected function _prepareData() |
91
|
|
|
{ |
92
|
|
|
|
93
|
|
|
if (!$this->_config->isEnabled()) { |
94
|
|
|
return parent::_prepareData(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$product = $this->_coreRegistry->registry('product'); |
98
|
|
|
$categoryIds = $this->_upsell->getCategoryIds($product); |
99
|
|
|
$personalisedIds = $this->_upsell->getProductCollection([$product->getId()], $categoryIds); |
100
|
|
|
|
101
|
|
|
if (!$personalisedIds) { |
102
|
|
|
return parent::_prepareData(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->_isPredictedResults = true; |
106
|
|
|
|
107
|
|
|
$collection = $this->_upsell->getPersonalisedProductCollection($personalisedIds); |
108
|
|
|
|
109
|
|
|
$this->_itemCollection = $collection; |
110
|
|
|
|
111
|
|
|
if ($this->moduleManager->isEnabled('Magento_Checkout')) { |
112
|
|
|
$this->_addProductAttributesAndPrices($this->_itemCollection); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->_eventManager->dispatch( |
116
|
|
|
'catalog_product_upsell', |
117
|
|
|
['product' => $product, 'collection' => $this->_itemCollection, 'limit' => null] |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
foreach ($this->_itemCollection as $product) { |
|
|
|
|
121
|
|
|
$product->setDoNotUseCategoryId(true); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the URL for product upsells block via ajax |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getUpsellAjaxUrl() |
133
|
|
|
{ |
134
|
|
|
return $this->getBaseUrl() . "personalised/products/upsellAjax/id/" . $this->getProduct()->getId(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check if the displayed results are from PredictionIO |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function isPredictedResults() |
143
|
|
|
{ |
144
|
|
|
return $this->_isPredictedResults; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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.