View.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Smartwave\Porto\Helper\Product;
  8. use Magento\Framework\View\Result\Page as ResultPage;
  9. /**
  10. * Catalog category helper
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class View extends \Magento\Catalog\Helper\Product\View
  14. {
  15. /**
  16. * List of catalog product session message groups
  17. *
  18. * @var array
  19. */
  20. protected $messageGroups;
  21. /**
  22. * Core registry
  23. *
  24. * @var \Magento\Framework\Registry
  25. */
  26. protected $_coreRegistry = null;
  27. /**
  28. * Catalog product
  29. *
  30. * @var \Magento\Catalog\Helper\Product
  31. */
  32. protected $_catalogProduct = null;
  33. /**
  34. * Catalog design
  35. *
  36. * @var \Magento\Catalog\Model\Design
  37. */
  38. protected $_catalogDesign;
  39. /**
  40. * Catalog session
  41. *
  42. * @var \Magento\Catalog\Model\Session
  43. */
  44. protected $_catalogSession;
  45. /**
  46. * @var \Magento\Framework\Message\ManagerInterface
  47. */
  48. protected $messageManager;
  49. /**
  50. * @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
  51. */
  52. protected $categoryUrlPathGenerator;
  53. protected $_scopeConfig;
  54. protected $_storeManager;
  55. /**
  56. * Constructor
  57. *
  58. * @param \Magento\Framework\App\Helper\Context $context
  59. * @param \Magento\Catalog\Model\Session $catalogSession
  60. * @param \Magento\Catalog\Model\Design $catalogDesign
  61. * @param \Magento\Catalog\Helper\Product $catalogProduct
  62. * @param \Magento\Framework\Registry $coreRegistry
  63. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  64. * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
  65. * @param array $messageGroups
  66. */
  67. public function __construct(
  68. \Magento\Framework\App\Helper\Context $context,
  69. \Magento\Catalog\Model\Session $catalogSession,
  70. \Magento\Catalog\Model\Design $catalogDesign,
  71. \Magento\Catalog\Helper\Product $catalogProduct,
  72. \Magento\Framework\Registry $coreRegistry,
  73. \Magento\Framework\Message\ManagerInterface $messageManager,
  74. \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
  75. \Magento\Store\Model\StoreManagerInterface $storeManager,
  76. array $messageGroups = []
  77. ) {
  78. $this->_catalogSession = $catalogSession;
  79. $this->_catalogDesign = $catalogDesign;
  80. $this->_catalogProduct = $catalogProduct;
  81. $this->_coreRegistry = $coreRegistry;
  82. $this->messageGroups = $messageGroups;
  83. $this->messageManager = $messageManager;
  84. $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
  85. $this->_scopeConfig = $context->getScopeConfig();
  86. $this->_storeManager = $storeManager;
  87. parent::__construct($context, $catalogSession, $catalogDesign, $catalogProduct, $coreRegistry, $messageManager, $categoryUrlPathGenerator);
  88. }
  89. /**
  90. * Init layout for viewing product page
  91. *
  92. * @param \Magento\Framework\View\Result\Page $resultPage
  93. * @param \Magento\Catalog\Model\Product $product
  94. * @param null|\Magento\Framework\DataObject $params
  95. * @return \Magento\Catalog\Helper\Product\View
  96. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  97. * @SuppressWarnings(PHPMD.NPathComplexity)
  98. */
  99. public function initProductLayout(ResultPage $resultPage, $product, $params = null)
  100. {
  101. $settings = $this->_catalogDesign->getDesignSettings($product);
  102. $pageConfig = $resultPage->getConfig();
  103. if ($settings->getCustomDesign()) {
  104. $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign());
  105. }
  106. // Apply custom page layout
  107. if ($settings->getPageLayout()) {
  108. $pageConfig->setPageLayout($settings->getPageLayout());
  109. }else{
  110. $panelLayout = $this->_scopeConfig->getValue('porto_settings/product/page_layout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->_storeManager->getStore()->getId());
  111. if($panelLayout!=''){
  112. $pageConfig->setPageLayout($panelLayout);
  113. }
  114. }
  115. $urlSafeSku = rawurlencode($product->getSku());
  116. // Load default page handles and page configurations
  117. if ($params && $params->getBeforeHandles()) {
  118. foreach ($params->getBeforeHandles() as $handle) {
  119. $resultPage->addPageLayoutHandles(
  120. ['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()],
  121. $handle
  122. );
  123. }
  124. }
  125. $resultPage->addPageLayoutHandles(
  126. ['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()]
  127. );
  128. if ($params && $params->getAfterHandles()) {
  129. foreach ($params->getAfterHandles() as $handle) {
  130. $resultPage->addPageLayoutHandles(
  131. ['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()],
  132. $handle
  133. );
  134. }
  135. }
  136. // Apply custom layout update once layout is loaded
  137. $update = $resultPage->getLayout()->getUpdate();
  138. $layoutUpdates = $settings->getLayoutUpdates();
  139. if ($layoutUpdates) {
  140. if (is_array($layoutUpdates)) {
  141. foreach ($layoutUpdates as $layoutUpdate) {
  142. $update->addUpdate($layoutUpdate);
  143. }
  144. }
  145. }
  146. $currentCategory = $this->_coreRegistry->registry('current_category');
  147. $controllerClass = $this->_request->getFullActionName();
  148. if ($controllerClass != 'catalog-product-view') {
  149. $pageConfig->addBodyClass('catalog-product-view');
  150. }
  151. $full_width = $this->_scopeConfig->getValue('porto_settings/general/layout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->_storeManager->getStore()->getId());
  152. $product_page_type = $product->getData('product_page_type');
  153. if(!$product_page_type)
  154. $product_page_type = $this->_scopeConfig->getValue('porto_settings/product/product_page_type', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->_storeManager->getStore()->getId());
  155. $additional_class = '';
  156. if(isset($full_width) && $full_width == 'full_width')
  157. $additional_class = 'layout-fullwidth';
  158. if(isset($product_page_type) && $product_page_type)
  159. $pageConfig->addBodyClass('product-type-'.$product_page_type);
  160. $pageConfig->addBodyClass('product-' . $product->getUrlKey())
  161. ->addBodyClass($additional_class);
  162. if ($currentCategory instanceof \Magento\Catalog\Model\Category) {
  163. $pageConfig->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($currentCategory))
  164. ->addBodyClass('category-' . $currentCategory->getUrlKey());
  165. }
  166. return $this;
  167. }
  168. }