-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathData.php
613 lines (547 loc) · 19.2 KB
/
Data.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<?php
/**
* Copyright 2011 Adobe
* All Rights Reserved.
*/
namespace Magento\Catalog\Helper;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Tax\Api\Data\TaxClassKeyInterface;
use Magento\Tax\Model\Config;
/**
* Catalog data helper
*
* @api
*
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
* @since 100.0.2
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public const PRICE_SCOPE_GLOBAL = 0;
public const PRICE_SCOPE_WEBSITE = 1;
public const XML_PATH_PRICE_SCOPE = 'catalog/price/scope';
public const CONFIG_USE_STATIC_URLS = 'cms/wysiwyg/use_static_urls_in_catalog';
/**
* @deprecated
* @see \Magento\Catalog\Helper\Output::isDirectivesExists
*/
public const CONFIG_PARSE_URL_DIRECTIVES = 'catalog/frontend/parse_url_directives';
public const XML_PATH_DISPLAY_PRODUCT_COUNT = 'catalog/layered_navigation/display_product_count';
/**
* Cache context
*/
public const CONTEXT_CATALOG_SORT_DIRECTION = 'catalog_sort_direction';
public const CONTEXT_CATALOG_SORT_ORDER = 'catalog_sort_order';
public const CONTEXT_CATALOG_DISPLAY_MODE = 'catalog_mode';
public const CONTEXT_CATALOG_LIMIT = 'catalog_limit';
/**
* Breadcrumb Path cache
*
* @var array
*/
protected $_categoryPath;
/**
* Currently selected store ID if applicable
*
* @var int
*/
protected $_storeId;
/**
* Framework Core registry
*
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry;
/**
* @var Product
*/
protected $_catalogProduct;
/**
* @var Category
*/
protected $_catalogCategory;
/**
* @var \Magento\Framework\Stdlib\StringUtils
*/
protected $string;
/**
* @var string
*/
protected $_templateFilterModel;
/**
* @var \Magento\Catalog\Model\Session
*/
protected $_catalogSession;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Catalog\Model\Template\Filter\Factory
*/
protected $_templateFilterFactory;
/**
* @var \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory
*/
protected $_taxClassKeyFactory;
/**
* @var \Magento\Tax\Model\Config
*/
protected $_taxConfig;
/**
* @var \Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory
*/
protected $_quoteDetailsFactory;
/**
* @var \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory
*/
protected $_quoteDetailsItemFactory;
/**
* @var CustomerSession
*/
protected $_customerSession;
/**
* Tax calculation service interface
*
* @var \Magento\Tax\Api\TaxCalculationInterface
*/
protected $_taxCalculationService;
/**
* @var PriceCurrencyInterface
*/
protected $priceCurrency;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var CategoryRepositoryInterface
*/
protected $categoryRepository;
/**
* @var \Magento\Customer\Api\GroupRepositoryInterface
*/
protected $customerGroupRepository;
/**
* @var \Magento\Customer\Api\Data\AddressInterfaceFactory
*/
protected $addressFactory;
/**
* @var \Magento\Customer\Api\Data\RegionInterfaceFactory
*/
protected $regionFactory;
/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Catalog\Model\Session $catalogSession
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param Category $catalogCategory
* @param Product $catalogProduct
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Catalog\Model\Template\Filter\Factory $templateFilterFactory
* @param string $templateFilterModel
* @param \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyFactory
* @param Config $taxConfig
* @param \Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory $quoteDetailsFactory
* @param \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory
* @param \Magento\Tax\Api\TaxCalculationInterface $taxCalculationService
* @param CustomerSession $customerSession
* @param PriceCurrencyInterface $priceCurrency
* @param ProductRepositoryInterface $productRepository
* @param CategoryRepositoryInterface $categoryRepository
* @param \Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository
* @param \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory
* @param \Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\Session $catalogSession,
\Magento\Framework\Stdlib\StringUtils $string,
Category $catalogCategory,
Product $catalogProduct,
\Magento\Framework\Registry $coreRegistry,
\Magento\Catalog\Model\Template\Filter\Factory $templateFilterFactory,
$templateFilterModel,
\Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyFactory,
\Magento\Tax\Model\Config $taxConfig,
\Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory $quoteDetailsFactory,
\Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory,
\Magento\Tax\Api\TaxCalculationInterface $taxCalculationService,
CustomerSession $customerSession,
PriceCurrencyInterface $priceCurrency,
ProductRepositoryInterface $productRepository,
CategoryRepositoryInterface $categoryRepository,
\Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository,
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory,
\Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory
) {
$this->_storeManager = $storeManager;
$this->_catalogSession = $catalogSession;
$this->_templateFilterFactory = $templateFilterFactory;
$this->string = $string;
$this->_catalogCategory = $catalogCategory;
$this->_catalogProduct = $catalogProduct;
$this->_coreRegistry = $coreRegistry;
$this->_templateFilterModel = $templateFilterModel;
$this->_taxClassKeyFactory = $taxClassKeyFactory;
$this->_taxConfig = $taxConfig;
$this->_quoteDetailsFactory = $quoteDetailsFactory;
$this->_quoteDetailsItemFactory = $quoteDetailsItemFactory;
$this->_taxCalculationService = $taxCalculationService;
$this->_customerSession = $customerSession;
$this->priceCurrency = $priceCurrency;
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->customerGroupRepository = $customerGroupRepository;
$this->addressFactory = $addressFactory;
$this->regionFactory = $regionFactory;
parent::__construct($context);
}
/**
* Set a specified store ID value
*
* @param int $store
* @return $this
*/
public function setStoreId($store)
{
$this->_storeId = $store;
return $this;
}
/**
* Return current category path or get it from current category
*
* Creating array of categories|product paths for breadcrumbs
*
* @return array
*/
public function getBreadcrumbPath()
{
if (!$this->_categoryPath) {
$path = [];
$category = $this->getCategory();
if ($category) {
$pathInStore = $category->getPathInStore();
$pathIds = array_reverse(explode(',', $pathInStore));
$categories = $category->getParentCategories();
// add category path breadcrumb
foreach ($pathIds as $categoryId) {
if (isset($categories[$categoryId]) && $categories[$categoryId]->getName()) {
$path['category' . $categoryId] = [
'label' => $categories[$categoryId]->getName(),
'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getUrl() : ''
];
}
}
}
if ($this->getProduct()) {
$path['product'] = ['label' => $this->getProduct()->getName()];
}
$this->_categoryPath = $path;
}
return $this->_categoryPath;
}
/**
* Check is category link
*
* @param int $categoryId
* @return bool
*/
protected function _isCategoryLink($categoryId)
{
if ($this->getProduct()) {
return true;
}
if ($categoryId != $this->getCategory()->getId()) {
return true;
}
return false;
}
/**
* Return current category object
*
* @return \Magento\Catalog\Model\Category|null
*/
public function getCategory()
{
return $this->_coreRegistry->registry('current_category');
}
/**
* Retrieve current Product object
*
* @return \Magento\Catalog\Model\Product|null
*/
public function getProduct()
{
return $this->_coreRegistry->registry('current_product');
}
/**
* Retrieve Visitor/Customer Last Viewed URL
*
* @return string
*/
public function getLastViewedUrl()
{
$productId = $this->_catalogSession->getLastViewedProductId();
if ($productId) {
try {
$product = $this->productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
return '';
}
/* @var $product \Magento\Catalog\Model\Product */
if ($this->_catalogProduct->canShow($product, 'catalog')) {
return $product->getProductUrl();
}
}
$categoryId = $this->_catalogSession->getLastViewedCategoryId();
if ($categoryId) {
try {
$category = $this->categoryRepository->get($categoryId);
} catch (NoSuchEntityException $e) {
return '';
}
/* @var $category \Magento\Catalog\Model\Category */
if (!$this->_catalogCategory->canShow($category)) {
return '';
}
return $category->getCategoryUrl();
}
return '';
}
/**
* Split SKU of an item by dashes and spaces
*
* Words will not be broken, unless this length is greater than $length
*
* @param string $sku
* @param int $length
* @return string[]
*/
public function splitSku($sku, $length = 30)
{
return $this->string->split($sku, $length, true, false, '[\-\s]');
}
/**
* Retrieve attribute hidden fields
*
* @return array
*/
public function getAttributeHiddenFields()
{
if ($this->_coreRegistry->registry('attribute_type_hidden_fields')) {
return $this->_coreRegistry->registry('attribute_type_hidden_fields');
} else {
return [];
}
}
/**
* Retrieve Catalog Price Scope
*
* @return int|null
*/
public function getPriceScope(): ?int
{
$priceScope = $this->scopeConfig->getValue(
self::XML_PATH_PRICE_SCOPE,
ScopeInterface::SCOPE_STORE
);
return isset($priceScope) ? (int)$priceScope : null;
}
/**
* Is Global Price
*
* @return bool
*/
public function isPriceGlobal()
{
return $this->getPriceScope() == self::PRICE_SCOPE_GLOBAL;
}
/**
* Check if the store is configured to use static URLs for media
*
* @return bool
*/
public function isUsingStaticUrlsAllowed()
{
return $this->scopeConfig->isSetFlag(
self::CONFIG_USE_STATIC_URLS,
ScopeInterface::SCOPE_STORE
);
}
/**
* Check if the parsing of URL directives is allowed for the catalog
*
* @return bool
* @deprecated 103.0.0
* @see \Magento\Catalog\Helper\Output::isDirectivesExists
*/
public function isUrlDirectivesParsingAllowed()
{
return $this->scopeConfig->isSetFlag(
self::CONFIG_PARSE_URL_DIRECTIVES,
ScopeInterface::SCOPE_STORE,
$this->_storeId
);
}
/**
* Retrieve template processor for catalog content
*
* @return \Magento\Framework\Filter\Template
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getPageTemplateProcessor()
{
return $this->_templateFilterFactory->create($this->_templateFilterModel);
}
/**
* Whether to display items count for each filter option
*
* @param int $storeId Store view ID
* @return bool
*/
public function shouldDisplayProductCountOnLayer($storeId = null)
{
return $this->scopeConfig->isSetFlag(
self::XML_PATH_DISPLAY_PRODUCT_COUNT,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
/**
* Convert tax address array to address data object with country id and postcode
*
* @param array $taxAddress
* @return \Magento\Customer\Api\Data\AddressInterface|null
*/
private function convertDefaultTaxAddress(?array $taxAddress = null)
{
if (empty($taxAddress)) {
return null;
}
/** @var \Magento\Customer\Api\Data\AddressInterface $addressDataObject */
$addressDataObject = $this->addressFactory->create()
->setCountryId($taxAddress['country_id'])
->setPostcode($taxAddress['postcode']);
if (isset($taxAddress['region_id'])) {
$addressDataObject->setRegion($this->regionFactory->create()->setRegionId($taxAddress['region_id']));
}
return $addressDataObject;
}
/**
* Get product price with all tax settings processing
*
* @param \Magento\Catalog\Model\Product $product
* @param float $price inputted product price
* @param bool $includingTax return price include tax flag
* @param null|\Magento\Customer\Model\Address\AbstractAddress $shippingAddress
* @param null|\Magento\Customer\Model\Address\AbstractAddress $billingAddress
* @param null|int $ctc customer tax class
* @param null|string|bool|int|\Magento\Store\Model\Store $store
* @param bool $priceIncludesTax flag what price parameter contain tax
* @param bool $roundPrice
* @return float
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getTaxPrice(
$product,
$price,
$includingTax = null,
$shippingAddress = null,
$billingAddress = null,
$ctc = null,
$store = null,
$priceIncludesTax = null,
$roundPrice = true
) {
if (!$price) {
return $price;
}
$store = $this->_storeManager->getStore($store);
if ($this->_taxConfig->needPriceConversion($store)) {
if ($priceIncludesTax === null) {
$priceIncludesTax = $this->_taxConfig->priceIncludesTax($store);
}
$shippingAddressDataObject = null;
if ($shippingAddress === null) {
$shippingAddressDataObject =
$this->convertDefaultTaxAddress($this->_customerSession->getDefaultTaxShippingAddress());
} elseif ($shippingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) {
$shippingAddressDataObject = $shippingAddress->getDataModel();
}
$billingAddressDataObject = null;
if ($billingAddress === null) {
$billingAddressDataObject =
$this->convertDefaultTaxAddress($this->_customerSession->getDefaultTaxBillingAddress());
} elseif ($billingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) {
$billingAddressDataObject = $billingAddress->getDataModel();
}
$taxClassKey = $this->_taxClassKeyFactory->create();
$taxClassKey->setType(TaxClassKeyInterface::TYPE_ID)
->setValue($product->getTaxClassId());
if ($ctc === null && $this->_customerSession->getCustomerGroupId() !== null) {
$ctc = $this->customerGroupRepository->getById($this->_customerSession->getCustomerGroupId())
->getTaxClassId();
}
$customerTaxClassKey = $this->_taxClassKeyFactory->create();
$customerTaxClassKey->setType(TaxClassKeyInterface::TYPE_ID)
->setValue($ctc);
$item = $this->_quoteDetailsItemFactory->create();
$item->setQuantity(1)
->setCode($product->getSku())
->setShortDescription($product->getShortDescription())
->setTaxClassKey($taxClassKey)
->setIsTaxIncluded($priceIncludesTax)
->setType('product')
->setUnitPrice($price);
$quoteDetails = $this->_quoteDetailsFactory->create();
$quoteDetails->setShippingAddress($shippingAddressDataObject)
->setBillingAddress($billingAddressDataObject)
->setCustomerTaxClassKey($customerTaxClassKey)
->setItems([$item])
->setCustomerId($this->_customerSession->getCustomerId());
$storeId = null;
if ($store) {
$storeId = $store->getId();
}
$taxDetails = $this->_taxCalculationService->calculateTax($quoteDetails, $storeId, $roundPrice);
$items = $taxDetails->getItems();
$taxDetailsItem = array_shift($items);
if ($includingTax !== null) {
if ($includingTax) {
$price = $taxDetailsItem->getPriceInclTax();
} else {
$price = $taxDetailsItem->getPrice();
}
} else {
switch ($this->_taxConfig->getPriceDisplayType($store)) {
case Config::DISPLAY_TYPE_EXCLUDING_TAX:
case Config::DISPLAY_TYPE_BOTH:
$price = $taxDetailsItem->getPrice();
break;
case Config::DISPLAY_TYPE_INCLUDING_TAX:
$price = $taxDetailsItem->getPriceInclTax();
break;
default:
break;
}
}
}
if ($roundPrice) {
return $this->priceCurrency->round($price);
} else {
return $price;
}
}
}