Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Helper;
7 
15 
26 {
27  const PRICE_SCOPE_GLOBAL = 0;
28 
30 
31  const XML_PATH_PRICE_SCOPE = 'catalog/price/scope';
32 
33  const CONFIG_USE_STATIC_URLS = 'cms/wysiwyg/use_static_urls_in_catalog';
34 
39  const CONFIG_PARSE_URL_DIRECTIVES = 'catalog/frontend/parse_url_directives';
40 
41  const XML_PATH_DISPLAY_PRODUCT_COUNT = 'catalog/layered_navigation/display_product_count';
42 
46  const CONTEXT_CATALOG_SORT_DIRECTION = 'catalog_sort_direction';
47 
48  const CONTEXT_CATALOG_SORT_ORDER = 'catalog_sort_order';
49 
50  const CONTEXT_CATALOG_DISPLAY_MODE = 'catalog_mode';
51 
52  const CONTEXT_CATALOG_LIMIT = 'catalog_limit';
53 
59  protected $_categoryPath;
60 
66  protected $_storeId;
67 
73  protected $_coreRegistry;
74 
80  protected $_catalogProduct;
81 
87  protected $_catalogCategory;
88 
92  protected $string;
93 
98 
104  protected $_catalogSession;
105 
111  protected $_storeManager;
112 
119 
126 
132  protected $_taxConfig;
133 
140 
147 
151  protected $_customerSession;
152 
159 
165  protected $priceCurrency;
166 
171 
176 
181 
185  protected $addressFactory;
186 
190  protected $regionFactory;
191 
216  public function __construct(
217  \Magento\Framework\App\Helper\Context $context,
218  \Magento\Store\Model\StoreManagerInterface $storeManager,
219  \Magento\Catalog\Model\Session $catalogSession,
220  \Magento\Framework\Stdlib\StringUtils $string,
221  Category $catalogCategory,
222  Product $catalogProduct,
223  \Magento\Framework\Registry $coreRegistry,
224  \Magento\Catalog\Model\Template\Filter\Factory $templateFilterFactory,
225  $templateFilterModel,
226  \Magento\Tax\Api\Data\TaxClassKeyInterfaceFactory $taxClassKeyFactory,
227  \Magento\Tax\Model\Config $taxConfig,
228  \Magento\Tax\Api\Data\QuoteDetailsInterfaceFactory $quoteDetailsFactory,
229  \Magento\Tax\Api\Data\QuoteDetailsItemInterfaceFactory $quoteDetailsItemFactory,
230  \Magento\Tax\Api\TaxCalculationInterface $taxCalculationService,
231  CustomerSession $customerSession,
235  \Magento\Customer\Api\GroupRepositoryInterface $customerGroupRepository,
236  \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory,
237  \Magento\Customer\Api\Data\RegionInterfaceFactory $regionFactory
238  ) {
239  $this->_storeManager = $storeManager;
240  $this->_catalogSession = $catalogSession;
241  $this->_templateFilterFactory = $templateFilterFactory;
242  $this->string = $string;
243  $this->_catalogCategory = $catalogCategory;
244  $this->_catalogProduct = $catalogProduct;
245  $this->_coreRegistry = $coreRegistry;
246  $this->_templateFilterModel = $templateFilterModel;
247  $this->_taxClassKeyFactory = $taxClassKeyFactory;
248  $this->_taxConfig = $taxConfig;
249  $this->_quoteDetailsFactory = $quoteDetailsFactory;
250  $this->_quoteDetailsItemFactory = $quoteDetailsItemFactory;
251  $this->_taxCalculationService = $taxCalculationService;
252  $this->_customerSession = $customerSession;
253  $this->priceCurrency = $priceCurrency;
254  $this->productRepository = $productRepository;
255  $this->categoryRepository = $categoryRepository;
256  $this->customerGroupRepository = $customerGroupRepository;
257  $this->addressFactory = $addressFactory;
258  $this->regionFactory = $regionFactory;
259  parent::__construct($context);
260  }
261 
268  public function setStoreId($store)
269  {
270  $this->_storeId = $store;
271  return $this;
272  }
273 
280  public function getBreadcrumbPath()
281  {
282  if (!$this->_categoryPath) {
283  $path = [];
284  $category = $this->getCategory();
285  if ($category) {
286  $pathInStore = $category->getPathInStore();
287  $pathIds = array_reverse(explode(',', $pathInStore));
288 
289  $categories = $category->getParentCategories();
290 
291  // add category path breadcrumb
292  foreach ($pathIds as $categoryId) {
293  if (isset($categories[$categoryId]) && $categories[$categoryId]->getName()) {
294  $path['category' . $categoryId] = [
295  'label' => $categories[$categoryId]->getName(),
296  'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getUrl() : ''
297  ];
298  }
299  }
300  }
301 
302  if ($this->getProduct()) {
303  $path['product'] = ['label' => $this->getProduct()->getName()];
304  }
305 
306  $this->_categoryPath = $path;
307  }
308  return $this->_categoryPath;
309  }
310 
317  protected function _isCategoryLink($categoryId)
318  {
319  if ($this->getProduct()) {
320  return true;
321  }
322  if ($categoryId != $this->getCategory()->getId()) {
323  return true;
324  }
325  return false;
326  }
327 
333  public function getCategory()
334  {
335  return $this->_coreRegistry->registry('current_category');
336  }
337 
343  public function getProduct()
344  {
345  return $this->_coreRegistry->registry('current_product');
346  }
347 
353  public function getLastViewedUrl()
354  {
355  $productId = $this->_catalogSession->getLastViewedProductId();
356  if ($productId) {
357  try {
358  $product = $this->productRepository->getById($productId);
359  } catch (NoSuchEntityException $e) {
360  return '';
361  }
362  /* @var $product \Magento\Catalog\Model\Product */
363  if ($this->_catalogProduct->canShow($product, 'catalog')) {
364  return $product->getProductUrl();
365  }
366  }
367  $categoryId = $this->_catalogSession->getLastViewedCategoryId();
368  if ($categoryId) {
369  try {
370  $category = $this->categoryRepository->get($categoryId);
371  } catch (NoSuchEntityException $e) {
372  return '';
373  }
374  /* @var $category \Magento\Catalog\Model\Category */
375  if (!$this->_catalogCategory->canShow($category)) {
376  return '';
377  }
378  return $category->getCategoryUrl();
379  }
380  return '';
381  }
382 
391  public function splitSku($sku, $length = 30)
392  {
393  return $this->string->split($sku, $length, true, false, '[\-\s]');
394  }
395 
401  public function getAttributeHiddenFields()
402  {
403  if ($this->_coreRegistry->registry('attribute_type_hidden_fields')) {
404  return $this->_coreRegistry->registry('attribute_type_hidden_fields');
405  } else {
406  return [];
407  }
408  }
409 
415  public function getPriceScope()
416  {
417  return $this->scopeConfig->getValue(
418  self::XML_PATH_PRICE_SCOPE,
420  );
421  }
422 
428  public function isPriceGlobal()
429  {
430  return $this->getPriceScope() == self::PRICE_SCOPE_GLOBAL;
431  }
432 
438  public function isUsingStaticUrlsAllowed()
439  {
440  return $this->scopeConfig->isSetFlag(
441  self::CONFIG_USE_STATIC_URLS,
443  );
444  }
445 
454  {
455  return $this->scopeConfig->isSetFlag(
456  self::CONFIG_PARSE_URL_DIRECTIVES,
458  $this->_storeId
459  );
460  }
461 
468  public function getPageTemplateProcessor()
469  {
470  return $this->_templateFilterFactory->create($this->_templateFilterModel);
471  }
472 
479  {
480  return $this->scopeConfig->isSetFlag(
481  self::XML_PATH_DISPLAY_PRODUCT_COUNT,
483  $storeId
484  );
485  }
486 
491  private function convertDefaultTaxAddress(array $taxAddress = null)
492  {
493  if (empty($taxAddress)) {
494  return null;
495  }
497  $addressDataObject = $this->addressFactory->create()
498  ->setCountryId($taxAddress['country_id'])
499  ->setPostcode($taxAddress['postcode']);
500 
501  if (isset($taxAddress['region_id'])) {
502  $addressDataObject->setRegion($this->regionFactory->create()->setRegionId($taxAddress['region_id']));
503  }
504  return $addressDataObject;
505  }
506 
524  public function getTaxPrice(
525  $product,
526  $price,
527  $includingTax = null,
528  $shippingAddress = null,
529  $billingAddress = null,
530  $ctc = null,
531  $store = null,
532  $priceIncludesTax = null,
533  $roundPrice = true
534  ) {
535  if (!$price) {
536  return $price;
537  }
538 
539  $store = $this->_storeManager->getStore($store);
540  if ($this->_taxConfig->needPriceConversion($store)) {
541  if ($priceIncludesTax === null) {
542  $priceIncludesTax = $this->_taxConfig->priceIncludesTax($store);
543  }
544 
546  if ($shippingAddress === null) {
548  $this->convertDefaultTaxAddress($this->_customerSession->getDefaultTaxShippingAddress());
549  } elseif ($shippingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) {
551  }
552 
554  if ($billingAddress === null) {
556  $this->convertDefaultTaxAddress($this->_customerSession->getDefaultTaxBillingAddress());
557  } elseif ($billingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) {
558  $billingAddressDataObject = $billingAddress->getDataModel();
559  }
560 
561  $taxClassKey = $this->_taxClassKeyFactory->create();
562  $taxClassKey->setType(TaxClassKeyInterface::TYPE_ID)
563  ->setValue($product->getTaxClassId());
564 
565  if ($ctc === null && $this->_customerSession->getCustomerGroupId() != null) {
566  $ctc = $this->customerGroupRepository->getById($this->_customerSession->getCustomerGroupId())
567  ->getTaxClassId();
568  }
569 
570  $customerTaxClassKey = $this->_taxClassKeyFactory->create();
571  $customerTaxClassKey->setType(TaxClassKeyInterface::TYPE_ID)
572  ->setValue($ctc);
573 
574  $item = $this->_quoteDetailsItemFactory->create();
575  $item->setQuantity(1)
576  ->setCode($product->getSku())
577  ->setShortDescription($product->getShortDescription())
578  ->setTaxClassKey($taxClassKey)
579  ->setIsTaxIncluded($priceIncludesTax)
580  ->setType('product')
581  ->setUnitPrice($price);
582 
583  $quoteDetails = $this->_quoteDetailsFactory->create();
584  $quoteDetails->setShippingAddress($shippingAddressDataObject)
585  ->setBillingAddress($billingAddressDataObject)
586  ->setCustomerTaxClassKey($customerTaxClassKey)
587  ->setItems([$item])
588  ->setCustomerId($this->_customerSession->getCustomerId());
589 
590  $storeId = null;
591  if ($store) {
592  $storeId = $store->getId();
593  }
594  $taxDetails = $this->_taxCalculationService->calculateTax($quoteDetails, $storeId, $roundPrice);
595  $items = $taxDetails->getItems();
596  $taxDetailsItem = array_shift($items);
597 
598  if ($includingTax !== null) {
599  if ($includingTax) {
600  $price = $taxDetailsItem->getPriceInclTax();
601  } else {
602  $price = $taxDetailsItem->getPrice();
603  }
604  } else {
605  switch ($this->_taxConfig->getPriceDisplayType($store)) {
608  $price = $taxDetailsItem->getPrice();
609  break;
611  $price = $taxDetailsItem->getPriceInclTax();
612  break;
613  default:
614  break;
615  }
616  }
617  }
618 
619  if ($roundPrice) {
620  return $this->priceCurrency->round($price);
621  } else {
622  return $price;
623  }
624  }
625 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$billingAddress
Definition: order.php:25
const CONTEXT_CATALOG_SORT_ORDER
Definition: Data.php:48
$shippingAddress
Definition: order.php:40
splitSku($sku, $length=30)
Definition: Data.php:391
$storeManager
$price
_isCategoryLink($categoryId)
Definition: Data.php:317
const DISPLAY_TYPE_EXCLUDING_TAX
Definition: Config.php:102
const DISPLAY_TYPE_INCLUDING_TAX
Definition: Config.php:104
shouldDisplayProductCountOnLayer($storeId=null)
Definition: Data.php:478
const CONTEXT_CATALOG_SORT_DIRECTION
Definition: Data.php:46
const CONFIG_PARSE_URL_DIRECTIVES
Definition: Data.php:39
getTaxPrice( $product, $price, $includingTax=null, $shippingAddress=null, $billingAddress=null, $ctc=null, $store=null, $priceIncludesTax=null, $roundPrice=true)
Definition: Data.php:524
$categories
const CONTEXT_CATALOG_DISPLAY_MODE
Definition: Data.php:50
__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)
Definition: Data.php:216
const XML_PATH_DISPLAY_PRODUCT_COUNT
Definition: Data.php:41
$items