|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Richdynamix\PersonalisedProducts\Block\Product\ProductList; |
|
4
|
|
|
|
|
5
|
|
|
use \Magento\TargetRule\Block\Catalog\Product\ProductList\Upsell; |
|
|
|
|
|
|
6
|
|
|
use \Magento\Catalog\Block\Product\Context; |
|
7
|
|
|
use \Magento\TargetRule\Model\ResourceModel\Index; |
|
8
|
|
|
use \Magento\TargetRule\Helper\Data; |
|
9
|
|
|
use \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
|
10
|
|
|
use \Magento\Catalog\Model\Product\Visibility; |
|
11
|
|
|
use \Magento\TargetRule\Model\IndexFactory; |
|
12
|
|
|
use \Magento\Framework\Module\Manager as Manager; |
|
13
|
|
|
use \Magento\Checkout\Model\Cart; |
|
14
|
|
|
use \Richdynamix\PersonalisedProducts\Helper\Config; |
|
15
|
|
|
use \Richdynamix\PersonalisedProducts\Model\Frontend\Catalog\Product\ProductList\Upsell as UpsellModel; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Rewrite target rule product upsell block in enterprise edition |
|
19
|
|
|
* to switch out product collection for one returned from PredictionIO |
|
20
|
|
|
* |
|
21
|
|
|
* @category Richdynamix |
|
22
|
|
|
* @package PersonalisedProducts |
|
23
|
|
|
* @author Steven Richardson ([email protected]) @mage_gizmo |
|
24
|
|
|
*/ |
|
25
|
|
|
class TargetUpsell extends Upsell |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var Cart |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_cart; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Config |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $_config; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var UpsellModel |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $_upsell; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var Manager |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $_moduleManager; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Context $context |
|
46
|
|
|
* @param Index $index |
|
47
|
|
|
* @param Data $targetRuleData |
|
48
|
|
|
* @param CollectionFactory $productCollectionFactory |
|
49
|
|
|
* @param Visibility $visibility |
|
50
|
|
|
* @param IndexFactory $indexFactory |
|
51
|
|
|
* @param Cart $cart |
|
52
|
|
|
* @param Config $config |
|
53
|
|
|
* @param UpsellModel $upsell |
|
54
|
|
|
* @param Manager $moduleManager |
|
55
|
|
|
*/ |
|
56
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
57
|
|
|
Context $context, |
|
58
|
|
|
Index $index, |
|
59
|
|
|
Data $targetRuleData, |
|
60
|
|
|
CollectionFactory $productCollectionFactory, |
|
61
|
|
|
Visibility $visibility, |
|
62
|
|
|
IndexFactory $indexFactory, |
|
63
|
|
|
Cart $cart, |
|
64
|
|
|
Config $config, |
|
65
|
|
|
UpsellModel $upsell, |
|
66
|
|
|
Manager $moduleManager |
|
67
|
|
|
) { |
|
68
|
|
|
$this->_cart = $cart; |
|
69
|
|
|
$this->_config = $config; |
|
70
|
|
|
$this->_upsell = $upsell; |
|
71
|
|
|
$this->_moduleManager = $moduleManager; |
|
72
|
|
|
parent::__construct( |
|
73
|
|
|
$context, |
|
74
|
|
|
$index, |
|
75
|
|
|
$targetRuleData, |
|
76
|
|
|
$productCollectionFactory, |
|
77
|
|
|
$visibility, |
|
78
|
|
|
$indexFactory, |
|
79
|
|
|
$cart |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Rewrite parent getAllItems method to use PredictionIO results when available |
|
85
|
|
|
* Rewrites the target rules for Enterprise edition |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getAllItems() |
|
90
|
|
|
{ |
|
91
|
|
|
if (!$this->_config->isEnabled() || !$this->_config->getItem(Config::SIMILARITY_REPLACE_RULES)) { |
|
92
|
|
|
return parent::getAllItems(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$product = $this->_coreRegistry->registry('product'); |
|
96
|
|
|
$categoryIds = $this->_upsell->getCategoryIds($product); |
|
97
|
|
|
$personalisedIds = $this->_upsell->getProductCollection([$product->getId()], $categoryIds); |
|
98
|
|
|
|
|
99
|
|
|
if (!$personalisedIds) { |
|
100
|
|
|
return parent::getAllItems(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$collection = $this->_upsell->getPersonalisedProductCollection($personalisedIds); |
|
104
|
|
|
|
|
105
|
|
|
if ($this->_moduleManager->isEnabled('Magento_Checkout')) { |
|
106
|
|
|
$this->_addProductAttributesAndPrices($collection); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$items = []; |
|
110
|
|
|
foreach ($collection as $product) { |
|
|
|
|
|
|
111
|
|
|
$product->setDoNotUseCategoryId(true); |
|
112
|
|
|
$items[] = $product; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $items; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: