-
Notifications
You must be signed in to change notification settings - Fork 16
/
Featured.php
173 lines (156 loc) · 5.71 KB
/
Featured.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
* @copyright Copyright (c) 2017-2022 Tweakwise.com B.V. (https://www.tweakwise.com)
* @license Proprietary and confidential, Unauthorized copying of this file, via any medium is strictly prohibited
*/
namespace Tweakwise\Magento2Tweakwise\Block\Catalog\Product\ProductList;
use Tweakwise\Magento2Tweakwise\Exception\ApiException;
use Tweakwise\Magento2Tweakwise\MagentoCompat\PreparePostDataFactory;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\Recommendation\Collection;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\Recommendation\Context as RecommendationsContext;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\Recommendations\FeaturedRequest;
use Tweakwise\Magento2Tweakwise\Model\Config;
use Tweakwise\Magento2Tweakwise\Model\Config\TemplateFinder;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Block\Product\Context as ProductContext;
use Magento\Catalog\Block\Product\ListProduct;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\Url\Helper\Data;
class Featured extends ListProduct
{
/**
* @var RecommendationsContext
*/
protected $recommendationsContext;
/**
* @var Config
*/
protected $config;
/**
* @var TemplateFinder
*/
protected $templateFinder;
/**
* @var PreparePostDataFactory
*/
protected $preparePostDataFactory;
/**
* Featured constructor.
*
* @param ProductContext $productContext
* @param PostHelper $postDataHelper
* @param Resolver $layerResolver
* @param TemplateFinder $templateFinder
* @param CategoryRepositoryInterface $categoryRepository
* @param Data $urlHelper
* @param RecommendationsContext $recommendationsContext
* @param Config $config
* @param PreparePostDataFactory $preparePostDataFactory
* @param array $data
* @internal param CategoryRepositoryInterface $categoryRepository
* @internal param Data $urlHelper
* @internal param RecommendationsContext $recommendationsContext
* @internal param Config $config
* @internal param array $data
*/
public function __construct(
ProductContext $productContext,
PostHelper $postDataHelper,
Resolver $layerResolver,
TemplateFinder $templateFinder,
CategoryRepositoryInterface $categoryRepository,
Data $urlHelper,
RecommendationsContext $recommendationsContext,
Config $config,
PreparePostDataFactory $preparePostDataFactory,
array $data = []
) {
parent::__construct(
$productContext,
$postDataHelper,
$layerResolver,
$categoryRepository,
$urlHelper,
$data
);
$this->recommendationsContext = $recommendationsContext;
$this->config = $config;
$this->templateFinder = $templateFinder;
$this->preparePostDataFactory = $preparePostDataFactory;
}
/**
* @return Collection
*/
public function getProductCollection()
{
return $this->_getProductCollection();
}
/**
* {@inheritdoc}
*/
public function toHtml()
{
/*
* Unfortunately class \Magento\Catalog\ViewModel\Product\Listing\PreparePostData
* does not exist in magento 2.3 but is used in magento 2.4.
* Magento_Catalog::product/list/items.phtml line 265 wants a PreparePostData from the block rendering the template
* PreparePostDataFactory tries to get an instance of that class if it is available, if it is
* we add it as a view model so that we remain compatible with magento 2.3 and lower.
*
* We dont add it if some view model is already registered
*/
if (!$this->getData('view_model') && $this->preparePostDataFactory->getPreparePostData()) {
$this->setData('view_model', $this->preparePostDataFactory->getPreparePostData());
}
try {
$this->_getProductCollection();
} catch (ApiException $e) {
return '';
}
return parent::toHtml();
}
/**
* {@inheritdoc}
*/
protected function _getProductCollection()
{
if ($this->_productCollection === null) {
if (!$this->checkRecommendationEnabled()) {
$this->_productCollection = parent::_getProductCollection()
->addFieldToFilter('entity_id', ['null' => true]);
} else {
$this->configureRequest($this->recommendationsContext->getRequest());
$this->_productCollection = $this->recommendationsContext
->getCollection();
}
}
return $this->_productCollection;
}
/**
* @return bool
*/
protected function checkRecommendationEnabled(): bool
{
return $this->config->isRecommendationsEnabled(Config::RECOMMENDATION_TYPE_FEATURED);
}
/**
* @param FeaturedRequest $request
*/
protected function configureRequest(FeaturedRequest $request)
{
$category = $this->_coreRegistry->registry('current_category');
if ($category instanceof Category) {
$templateId = $this->templateFinder->forCategory($category, Config::RECOMMENDATION_TYPE_FEATURED);
if (!$templateId) {
$templateId = $this->config->getRecommendationsTemplate(Config::RECOMMENDATION_TYPE_FEATURED);
}
} else {
$templateId = $this->config->getRecommendationsTemplate(Config::RECOMMENDATION_TYPE_FEATURED);
}
$request->setTemplate($templateId);
}
}