/
View.php
390 lines (353 loc) · 11.8 KB
/
View.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Block\Product;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
/**
* Product View block
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class View extends AbstractProduct implements \Magento\Framework\DataObject\IdentityInterface
{
/**
* Magento string lib
*
* @var \Magento\Framework\Stdlib\StringUtils
*/
protected $string;
/**
* @var \Magento\Framework\Json\EncoderInterface
*/
protected $_jsonEncoder;
/**
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
*/
protected $priceCurrency;
/**
* @var \Magento\Framework\Url\EncoderInterface
*/
protected $urlEncoder;
/**
* @var \Magento\Catalog\Helper\Product
*/
protected $_productHelper;
/**
* @var \Magento\Catalog\Model\ProductTypes\ConfigInterface
*/
protected $productTypeConfig;
/**
* @var \Magento\Framework\Locale\FormatInterface
*/
protected $_localeFormat;
/**
* @var \Magento\Customer\Model\Session
*/
protected $customerSession;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @param Context $context
* @param \Magento\Framework\Url\EncoderInterface $urlEncoder
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Magento\Catalog\Helper\Product $productHelper
* @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
* @param \Magento\Customer\Model\Session $customerSession
* @param ProductRepositoryInterface|\Magento\Framework\Pricing\PriceCurrencyInterface $productRepository
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
* @param array $data
* @codingStandardsIgnoreStart
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\Url\EncoderInterface $urlEncoder,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\Stdlib\StringUtils $string,
\Magento\Catalog\Helper\Product $productHelper,
\Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
\Magento\Framework\Locale\FormatInterface $localeFormat,
\Magento\Customer\Model\Session $customerSession,
ProductRepositoryInterface $productRepository,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
array $data = []
) {
$this->_productHelper = $productHelper;
$this->urlEncoder = $urlEncoder;
$this->_jsonEncoder = $jsonEncoder;
$this->productTypeConfig = $productTypeConfig;
$this->string = $string;
$this->_localeFormat = $localeFormat;
$this->customerSession = $customerSession;
$this->productRepository = $productRepository;
$this->priceCurrency = $priceCurrency;
parent::__construct(
$context,
$data
);
}
// @codingStandardsIgnoreEnd
/**
* Return wishlist widget options
*
* @return array
* @deprecated
*/
public function getWishlistOptions()
{
return ['productType' => $this->getProduct()->getTypeId()];
}
/**
* Add meta information from product to head block
*
* @return \Magento\Catalog\Block\Product\View
*/
protected function _prepareLayout()
{
$this->getLayout()->createBlock('Magento\Catalog\Block\Breadcrumbs');
$product = $this->getProduct();
if (!$product) {
return parent::_prepareLayout();
}
$title = $product->getMetaTitle();
if ($title) {
$this->pageConfig->getTitle()->set($title);
}
$keyword = $product->getMetaKeyword();
$currentCategory = $this->_coreRegistry->registry('current_category');
if ($keyword) {
$this->pageConfig->setKeywords($keyword);
} elseif ($currentCategory) {
$this->pageConfig->setKeywords($product->getName());
}
$description = $product->getMetaDescription();
if ($description) {
$this->pageConfig->setDescription($description);
} else {
$this->pageConfig->setDescription($this->string->substr(strip_tags($product->getDescription()), 0, 255));
}
if ($this->_productHelper->canUseCanonicalTag()) {
$this->pageConfig->addRemotePageAsset(
$product->getUrlModel()->getUrl($product, ['_ignore_category' => true]),
'canonical',
['attributes' => ['rel' => 'canonical']]
);
}
$pageMainTitle = $this->getLayout()->getBlock('page.main.title');
if ($pageMainTitle) {
$pageMainTitle->setPageTitle($product->getName());
}
return parent::_prepareLayout();
}
/**
* Retrieve current product model
*
* @return \Magento\Catalog\Model\Product
*/
public function getProduct()
{
if (!$this->_coreRegistry->registry('product') && $this->getProductId()) {
$product = $this->productRepository->getById($this->getProductId());
$this->_coreRegistry->register('product', $product);
}
return $this->_coreRegistry->registry('product');
}
/**
* Check if product can be emailed to friend
*
* @return bool
*/
public function canEmailToFriend()
{
return false;
}
/**
* Retrieve url for direct adding product to cart
*
* @param \Magento\Catalog\Model\Product $product
* @param array $additional
* @return string
*/
public function getAddToCartUrl($product, $additional = [])
{
if ($this->hasCustomAddToCartUrl()) {
return $this->getCustomAddToCartUrl();
}
if ($this->getRequest()->getParam('wishlist_next')) {
$additional['wishlist_next'] = 1;
}
$addUrlKey = \Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED;
$addUrlValue = $this->_urlBuilder->getUrl('*/*/*', ['_use_rewrite' => true, '_current' => true]);
$additional[$addUrlKey] = $this->urlEncoder->encode($addUrlValue);
return $this->_cartHelper->getAddUrl($product, $additional);
}
/**
* Get JSON encoded configuration array which can be used for JS dynamic
* price calculation depending on product options
*
* @return string
*/
public function getJsonConfig()
{
/* @var $product \Magento\Catalog\Model\Product */
$product = $this->getProduct();
if (!$this->hasOptions()) {
$config = [
'productId' => $product->getId(),
'priceFormat' => $this->_localeFormat->getPriceFormat()
];
return $this->_jsonEncoder->encode($config);
}
$tierPrices = [];
$tierPricesList = $product->getPriceInfo()->getPrice('tier_price')->getTierPriceList();
foreach ($tierPricesList as $tierPrice) {
$tierPrices[] = $this->priceCurrency->convert($tierPrice['price']->getValue());
}
$config = [
'productId' => $product->getId(),
'priceFormat' => $this->_localeFormat->getPriceFormat(),
'prices' => [
'oldPrice' => [
'amount' => $this->priceCurrency->convert(
$product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue()
),
'adjustments' => []
],
'basePrice' => [
'amount' => $this->priceCurrency->convert(
$product->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount()
),
'adjustments' => []
],
'finalPrice' => [
'amount' => $this->priceCurrency->convert(
$product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue()
),
'adjustments' => []
]
],
'idSuffix' => '_clone',
'tierPrices' => $tierPrices
];
$responseObject = new \Magento\Framework\DataObject();
$this->_eventManager->dispatch('catalog_product_view_config', ['response_object' => $responseObject]);
if (is_array($responseObject->getAdditionalOptions())) {
foreach ($responseObject->getAdditionalOptions() as $option => $value) {
$config[$option] = $value;
}
}
return $this->_jsonEncoder->encode($config);
}
/**
* Return true if product has options
*
* @return bool
*/
public function hasOptions()
{
if ($this->getProduct()->getTypeInstance()->hasOptions($this->getProduct())) {
return true;
}
return false;
}
/**
* Check if product has required options
*
* @return bool
*/
public function hasRequiredOptions()
{
return $this->getProduct()->getTypeInstance()->hasRequiredOptions($this->getProduct());
}
/**
* Define if setting of product options must be shown instantly.
* Used in case when options are usually hidden and shown only when user
* presses some button or link. In editing mode we better show these options
* instantly.
*
* @return bool
*/
public function isStartCustomization()
{
return $this->getProduct()->getConfigureMode() || $this->_request->getParam('startcustomization');
}
/**
* Get default qty - either as preconfigured, or as 1.
* Also restricts it by minimal qty.
*
* @param null|\Magento\Catalog\Model\Product $product
* @return int|float
*/
public function getProductDefaultQty($product = null)
{
if (!$product) {
$product = $this->getProduct();
}
$qty = $this->getMinimalQty($product);
$config = $product->getPreconfiguredValues();
$configQty = $config->getQty();
if ($configQty > $qty) {
$qty = $configQty;
}
return $qty;
}
/**
* Get container name, where product options should be displayed
*
* @return string
*/
public function getOptionsContainer()
{
return $this->getProduct()->getOptionsContainer() == 'container1' ? 'container1' : 'container2';
}
/**
* Check whether quantity field should be rendered
*
* @return bool
*/
public function shouldRenderQuantity()
{
return !$this->productTypeConfig->isProductSet($this->getProduct()->getTypeId());
}
/**
* Get Validation Rules for Quantity field
*
* @return array
*/
public function getQuantityValidators()
{
$validators = [];
$validators['required-number'] = true;
return $validators;
}
/**
* Return identifiers for produced content
*
* @return array
*/
public function getIdentities()
{
$identities = $this->getProduct()->getIdentities();
$category = $this->_coreRegistry->registry('current_category');
if ($category) {
$identities[] = Category::CACHE_TAG . '_' . $category->getId();
}
return $identities;
}
/**
* Retrieve customer data object
*
* @return int
*/
protected function getCustomerId()
{
return $this->customerSession->getCustomerId();
}
}