forked from algolia/algoliasearch-magento-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgolia.php
executable file
·231 lines (196 loc) · 6.31 KB
/
Algolia.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace Algolia\AlgoliaSearch\Block;
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
use Algolia\AlgoliaSearch\Helper\Configuration\PersonalizationHelper;
use Algolia\AlgoliaSearch\Helper\Data as CoreHelper;
use Algolia\AlgoliaSearch\Helper\Entity\CategoryHelper;
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
use Algolia\AlgoliaSearch\Helper\LandingPageHelper;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Data\CollectionDataSourceInterface;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Locale\Currency;
use Magento\Framework\Locale\Format;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Framework\Url\Helper\Data;
use Magento\Framework\View\Element\Template;
use Magento\Search\Helper\Data as CatalogSearchHelper;
class Algolia extends Template implements CollectionDataSourceInterface
{
private $config;
private $catalogSearchHelper;
private $registry;
private $productHelper;
private $currency;
private $format;
private $algoliaHelper;
private $urlHelper;
private $formKey;
private $httpContext;
private $coreHelper;
private $categoryHelper;
private $landingPageHelper;
private $personalizationHelper;
private $checkoutSession;
private $date;
private $priceKey;
public function __construct(
Template\Context $context,
ConfigHelper $config,
CatalogSearchHelper $catalogSearchHelper,
ProductHelper $productHelper,
Currency $currency,
Format $format,
Registry $registry,
AlgoliaHelper $algoliaHelper,
Data $urlHelper,
FormKey $formKey,
HttpContext $httpContext,
CoreHelper $coreHelper,
CategoryHelper $categoryHelper,
LandingPageHelper $landingPageHelper,
PersonalizationHelper $personalizationHelper,
CheckoutSession $checkoutSession,
DateTime $date,
array $data = []
) {
$this->config = $config;
$this->catalogSearchHelper = $catalogSearchHelper;
$this->productHelper = $productHelper;
$this->currency = $currency;
$this->format = $format;
$this->registry = $registry;
$this->algoliaHelper = $algoliaHelper;
$this->urlHelper = $urlHelper;
$this->formKey = $formKey;
$this->httpContext = $httpContext;
$this->coreHelper = $coreHelper;
$this->categoryHelper = $categoryHelper;
$this->landingPageHelper = $landingPageHelper;
$this->personalizationHelper = $personalizationHelper;
$this->checkoutSession = $checkoutSession;
$this->date = $date;
parent::__construct($context, $data);
}
/**
* @return \Magento\Store\Model\Store
*/
public function getStore()
{
/** @var \Magento\Store\Model\Store $store */
$store = $this->_storeManager->getStore();
return $store;
}
public function getConfigHelper()
{
return $this->config;
}
public function getCoreHelper()
{
return $this->coreHelper;
}
public function getProductHelper()
{
return $this->productHelper;
}
public function getCategoryHelper()
{
return $this->categoryHelper;
}
public function getCatalogSearchHelper()
{
return $this->catalogSearchHelper;
}
public function getAlgoliaHelper()
{
return $this->algoliaHelper;
}
public function getPersonalizationHelper()
{
return $this->personalizationHelper;
}
public function getCurrencySymbol()
{
return $this->currency->getCurrency($this->getCurrencyCode())->getSymbol();
}
public function getCurrencyCode()
{
return $this->getStore()->getCurrentCurrencyCode();
}
public function getPriceFormat()
{
return $this->format->getPriceFormat();
}
public function getGroupId()
{
return $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);
}
public function getPriceKey()
{
if ($this->priceKey === null) {
$currencyCode = $this->getCurrencyCode();
$this->priceKey = '.' . $currencyCode . '.default';
if ($this->config->isCustomerGroupsEnabled($this->getStore()->getStoreId())) {
$groupId = $this->getGroupId();
$this->priceKey = '.' . $currencyCode . '.group_' . $groupId;
}
}
return $this->priceKey;
}
public function getStoreId()
{
return $this->getStore()->getStoreId();
}
public function getCurrentCategory()
{
return $this->registry->registry('current_category');
}
/** @return \Magento\Catalog\Model\Product */
public function getCurrentProduct()
{
return $this->registry->registry('product');
}
/** @return \Magento\Sales\Model\Order */
public function getLastOrder()
{
return $this->checkoutSession->getLastRealOrder();
}
public function getAddToCartParams()
{
$url = $this->getAddToCartUrl();
return [
'action' => $url,
'formKey' => $this->formKey->getFormKey(),
];
}
public function getTimestamp()
{
return $this->date->gmtTimestamp('today midnight');
}
private function getAddToCartUrl($additional = [])
{
$continueUrl = $this->urlHelper->getEncodedUrl($this->_urlBuilder->getCurrentUrl());
$urlParamName = ActionInterface::PARAM_NAME_URL_ENCODED;
$routeParams = [
$urlParamName => $continueUrl,
'_secure' => $this->algoliaHelper->getRequest()->isSecure(),
];
if ($additional !== []) {
$routeParams = array_merge($routeParams, $additional);
}
return $this->_urlBuilder->getUrl('checkout/cart/add', $routeParams);
}
protected function getCurrentLandingPage()
{
$landingPageId = $this->getRequest()->getParam('landing_page_id');
if (!$landingPageId) {
return null;
}
return $this->landingPageHelper->getLandingPage($landingPageId);
}
}