/
AbstractProduct.php
509 lines (462 loc) · 13.8 KB
/
AbstractProduct.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Block\Product;
/**
* Class AbstractProduct
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AbstractProduct extends \Magento\Framework\View\Element\Template
{
/**
* @var array
*/
protected $_priceBlock = [];
/**
* Flag which allow/disallow to use link for as low as price
*
* @var bool
*/
protected $_useLinkForAsLowAs = true;
/**
* Default product amount per row
*
* @var int
*/
protected $_defaultColumnCount = 3;
/**
* Product amount per row depending on custom page layout of category
*
* @var array
*/
protected $_columnCountLayoutDepend = [];
/**
* Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry;
/**
* Tax data
*
* @var \Magento\Tax\Helper\Data
*/
protected $_taxData;
/**
* Catalog config
*
* @var \Magento\Catalog\Model\Config
*/
protected $_catalogConfig;
/**
* @var \Magento\Framework\Math\Random
*/
protected $_mathRandom;
/**
* @var \Magento\Checkout\Helper\Cart
*/
protected $_cartHelper;
/**
* @var \Magento\Wishlist\Helper\Data
*/
protected $_wishlistHelper;
/**
* @var \Magento\Catalog\Helper\Product\Compare
*/
protected $_compareProduct;
/**
* @var \Magento\Catalog\Helper\Image
*/
protected $_imageHelper;
/**
* @var ReviewRendererInterface
*/
protected $reviewRenderer;
/**
* @var \Magento\CatalogInventory\Api\StockRegistryInterface
*/
protected $stockRegistry;
/**
* @param Context $context
* @param array $data
*/
public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = [])
{
$this->_imageHelper = $context->getImageHelper();
$this->imageBuilder = $context->getImageBuilder();
$this->_compareProduct = $context->getCompareProduct();
$this->_wishlistHelper = $context->getWishlistHelper();
$this->_cartHelper = $context->getCartHelper();
$this->_catalogConfig = $context->getCatalogConfig();
$this->_coreRegistry = $context->getRegistry();
$this->_taxData = $context->getTaxData();
$this->_mathRandom = $context->getMathRandom();
$this->reviewRenderer = $context->getReviewRenderer();
$this->stockRegistry = $context->getStockRegistry();
parent::__construct($context, $data);
}
/**
* Retrieve url for add product to cart
* Will return product view page URL if product has required options
*
* @param \Magento\Catalog\Model\Product $product
* @param array $additional
* @return string
*/
public function getAddToCartUrl($product, $additional = [])
{
if ($product->getTypeInstance()->hasRequiredOptions($product)) {
if (!isset($additional['_escape'])) {
$additional['_escape'] = true;
}
if (!isset($additional['_query'])) {
$additional['_query'] = [];
}
$additional['_query']['options'] = 'cart';
return $this->getProductUrl($product, $additional);
}
return $this->_cartHelper->getAddUrl($product, $additional);
}
/**
* Retrieves url for form submitting.
*
* Some objects can use setSubmitRouteData() to set route and params for form submitting,
* otherwise default url will be used
*
* @param \Magento\Catalog\Model\Product $product
* @param array $additional
* @return string
*/
public function getSubmitUrl($product, $additional = [])
{
$submitRouteData = $this->getData('submit_route_data');
if ($submitRouteData) {
$route = $submitRouteData['route'];
$params = isset($submitRouteData['params']) ? $submitRouteData['params'] : [];
$submitUrl = $this->getUrl($route, array_merge($params, $additional));
} else {
$submitUrl = $this->getAddToCartUrl($product, $additional);
}
return $submitUrl;
}
/**
* Retrieve add to wishlist params
*
* @param \Magento\Catalog\Model\Product $product
* @return string
*/
public function getAddToWishlistParams($product)
{
return $this->_wishlistHelper->getAddParams($product);
}
/**
* Retrieve Add Product to Compare Products List URL
*
* @return string
*/
public function getAddToCompareUrl()
{
return $this->_compareProduct->getAddUrl();
}
/**
* Gets minimal sales quantity
*
* @param \Magento\Catalog\Model\Product $product
* @return int|null
*/
public function getMinimalQty($product)
{
$stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
$minSaleQty = $stockItem->getMinSaleQty();
return $minSaleQty > 0 ? $minSaleQty : null;
}
/**
* Get product reviews summary
*
* @param \Magento\Catalog\Model\Product $product
* @param bool $templateType
* @param bool $displayIfNoReviews
* @return string
*/
public function getReviewsSummaryHtml(
\Magento\Catalog\Model\Product $product,
$templateType = false,
$displayIfNoReviews = false
) {
return $this->reviewRenderer->getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
}
/**
* Retrieve currently viewed product object
*
* @return \Magento\Catalog\Model\Product
*/
public function getProduct()
{
if (!$this->hasData('product')) {
$this->setData('product', $this->_coreRegistry->registry('product'));
}
return $this->getData('product');
}
/**
* Add all attributes and apply pricing logic to products collection
* to get correct values in different products lists.
* E.g. crosssells, upsells, new products, recently viewed
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
protected function _addProductAttributesAndPrices(
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
) {
return $collection
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
->addUrlRewrite();
}
/**
* Retrieve Product URL using UrlDataObject
*
* @param \Magento\Catalog\Model\Product $product
* @param array $additional the route params
* @return string
*/
public function getProductUrl($product, $additional = [])
{
if ($this->hasProductUrl($product)) {
if (!isset($additional['_escape'])) {
$additional['_escape'] = true;
}
return $product->getUrlModel()->getUrl($product, $additional);
}
return '#';
}
/**
* Check Product has URL
*
* @param \Magento\Catalog\Model\Product $product
* @return bool
*/
public function hasProductUrl($product)
{
if ($product->getVisibleInSiteVisibilities()) {
return true;
}
if ($product->hasUrlDataObject()) {
if (in_array($product->hasUrlDataObject()->getVisibility(), $product->getVisibleInSiteVisibilities())) {
return true;
}
}
return false;
}
/**
* Retrieve product amount per row
*
* @return int
*/
public function getColumnCount()
{
if (!$this->_getData('column_count')) {
$pageLayout = $this->getPageLayout();
if ($pageLayout && $this->getColumnCountLayoutDepend($pageLayout->getCode())) {
$this->setData('column_count', $this->getColumnCountLayoutDepend($pageLayout->getCode()));
} else {
$this->setData('column_count', $this->_defaultColumnCount);
}
}
return (int) $this->_getData('column_count');
}
/**
* Add row size depends on page layout
*
* @param string $pageLayout
* @param int $columnCount
* @return \Magento\Catalog\Block\Product\ListProduct
*/
public function addColumnCountLayoutDepend($pageLayout, $columnCount)
{
$this->_columnCountLayoutDepend[$pageLayout] = $columnCount;
return $this;
}
/**
* Remove row size depends on page layout
*
* @param string $pageLayout
* @return \Magento\Catalog\Block\Product\ListProduct
*/
public function removeColumnCountLayoutDepend($pageLayout)
{
if (isset($this->_columnCountLayoutDepend[$pageLayout])) {
unset($this->_columnCountLayoutDepend[$pageLayout]);
}
return $this;
}
/**
* Retrieve row size depends on page layout
*
* @param string $pageLayout
* @return int|boolean
*/
public function getColumnCountLayoutDepend($pageLayout)
{
if (isset($this->_columnCountLayoutDepend[$pageLayout])) {
return $this->_columnCountLayoutDepend[$pageLayout];
}
return false;
}
/**
* Retrieve current page layout
*
* @return string
*/
public function getPageLayout()
{
// TODO: Implement of getting current page layout
return '';
}
/**
* Check whether the price can be shown for the specified product
*
* @param \Magento\Catalog\Model\Product $product
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getCanShowProductPrice($product)
{
return $product->getCanShowPrice() !== false;
}
/**
* Get if it is necessary to show product stock status
*
* @return bool
*/
public function displayProductStockStatus()
{
$statusInfo = new \Magento\Framework\DataObject(['display_status' => true]);
$this->_eventManager->dispatch('catalog_block_product_status_display', ['status' => $statusInfo]);
return (bool) $statusInfo->getDisplayStatus();
}
/**
* Get random string
*
* @param int $length
* @param string|null $chars
* @return string
*/
public function getRandomString($length, $chars = null)
{
return $this->_mathRandom->getRandomString($length, $chars);
}
/**
* Return HTML block with price
*
* @param \Magento\Catalog\Model\Product $product
* @return string
*/
public function getProductPrice(\Magento\Catalog\Model\Product $product)
{
return $this->getProductPriceHtml(
$product,
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
\Magento\Framework\Pricing\Render::ZONE_ITEM_LIST
);
}
/**
* Return HTML block with tier price
*
* @param \Magento\Catalog\Model\Product $product
* @param string $priceType
* @param string $renderZone
* @param array $arguments
* @return string
*/
public function getProductPriceHtml(
\Magento\Catalog\Model\Product $product,
$priceType,
$renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
array $arguments = []
) {
if (!isset($arguments['zone'])) {
$arguments['zone'] = $renderZone;
}
/** @var \Magento\Framework\Pricing\Render $priceRender */
$priceRender = $this->getLayout()->getBlock('product.price.render.default');
$price = '';
if ($priceRender) {
$price = $priceRender->render($priceType, $product, $arguments);
}
return $price;
}
/**
* Whether redirect to cart enabled
*
* @return bool
*/
public function isRedirectToCartEnabled()
{
return $this->_scopeConfig->getValue(
'checkout/cart/redirect_to_cart',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
/**
* Retrieve product details html
*
* @param \Magento\Catalog\Model\Product $product
* @return mixed
*/
public function getProductDetailsHtml(\Magento\Catalog\Model\Product $product)
{
$renderer = $this->getDetailsRenderer($product->getTypeId());
if ($renderer) {
$renderer->setProduct($product);
return $renderer->toHtml();
}
return '';
}
/**
* @param null $type
* @return bool|\Magento\Framework\View\Element\AbstractBlock
*/
public function getDetailsRenderer($type = null)
{
if ($type === null) {
$type = 'default';
}
$rendererList = $this->getDetailsRendererList();
if ($rendererList) {
return $rendererList->getRenderer($type, 'default');
}
return null;
}
/**
* @return \Magento\Framework\View\Element\RendererList
*/
protected function getDetailsRendererList()
{
return $this->getDetailsRendererListName() ? $this->getLayout()->getBlock(
$this->getDetailsRendererListName()
) : $this->getChildBlock(
'details.renderers'
);
}
/**
* Retrieve product image
*
* @param \Magento\Catalog\Model\Product $product
* @param string $imageId
* @param array $attributes
* @return \Magento\Catalog\Block\Product\Image
*/
public function getImage($product, $imageId, $attributes = [])
{
return $this->imageBuilder->setProduct($product)
->setImageId($imageId)
->setAttributes($attributes)
->create();
}
}