|
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
|
|
|
* Upsell constructor. |
|
43
|
|
|
* @param Context $context |
|
44
|
|
|
* @param Cart $checkoutCart |
|
45
|
|
|
* @param Visibility $productVisibility |
|
46
|
|
|
* @param Session $checkoutSession |
|
47
|
|
|
* @param Manager $moduleManager |
|
48
|
|
|
* @param ProductFactory $productFactory |
|
49
|
|
|
* @param Config $config |
|
50
|
|
|
* @param PersonalisedUpsell $upsell |
|
51
|
|
|
* @param CustomerSession $customerSession |
|
52
|
|
|
* @param array $data |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
Context $context, |
|
56
|
|
|
Cart $checkoutCart, |
|
57
|
|
|
Visibility $productVisibility, |
|
58
|
|
|
Session $checkoutSession, |
|
59
|
|
|
Manager $moduleManager, |
|
60
|
|
|
ProductFactory $productFactory, |
|
61
|
|
|
Config $config, |
|
62
|
|
|
PersonalisedUpsell $upsell, |
|
63
|
|
|
CustomerSession $customerSession, |
|
64
|
|
|
array $data = [] |
|
65
|
|
|
) { |
|
66
|
|
|
$this->_config = $config; |
|
67
|
|
|
$this->_productFactory = $productFactory; |
|
68
|
|
|
$this->_upsell = $upsell; |
|
69
|
|
|
$this->_customerSession = $customerSession; |
|
70
|
|
|
parent::__construct( |
|
71
|
|
|
$context, |
|
72
|
|
|
$checkoutCart, |
|
73
|
|
|
$productVisibility, |
|
74
|
|
|
$checkoutSession, |
|
75
|
|
|
$moduleManager, |
|
76
|
|
|
$data |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Rewrite parent _prepareData method to use PredictionIO results when available |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function _prepareData() |
|
86
|
|
|
{ |
|
87
|
|
|
|
|
88
|
|
|
if (!$this->_config->isEnabled()) { |
|
89
|
|
|
return parent::_prepareData(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$product = $this->_coreRegistry->registry('product'); |
|
93
|
|
|
$categoryIds = $this->_upsell->getCategoryIds($product); |
|
94
|
|
|
$personalisedIds = $this->_upsell->getProductCollection([$product->getId()], $categoryIds); |
|
95
|
|
|
|
|
96
|
|
|
if (!$personalisedIds) { |
|
97
|
|
|
return parent::_prepareData(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$collection = $this->_upsell->getPersonalisedProductCollection($personalisedIds); |
|
101
|
|
|
|
|
102
|
|
|
$this->_itemCollection = $collection; |
|
103
|
|
|
|
|
104
|
|
|
if ($this->moduleManager->isEnabled('Magento_Checkout')) { |
|
105
|
|
|
$this->_addProductAttributesAndPrices($this->_itemCollection); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->_eventManager->dispatch( |
|
109
|
|
|
'catalog_product_upsell', |
|
110
|
|
|
['product' => $product, 'collection' => $this->_itemCollection, 'limit' => null] |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
foreach ($this->_itemCollection as $product) { |
|
|
|
|
|
|
114
|
|
|
$product->setDoNotUseCategoryId(true); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the URL for product upsells block via ajax |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getUpsellAjaxUrl() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getBaseUrl() . "personalised/products/upsellAjax/id/" . $this->getProduct()->getId(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|