custom/plugins/HatslogicCategoryWiseHidePrice/src/Subscriber/ProductSubscriber.php line 105

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3. * (c) 2Hats Logic Solutions <info@2hatslogic.com>
  4. */
  5. namespace Hatslogic\CategoryWiseHidePrice\Subscriber;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Doctrine\DBAL\Connection;
  8. use Shopware\Core\Framework\Context;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\Routing\RouterInterface;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\Content\Product\ProductEvents;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  17. use Shopware\Core\Content\Product\ProductEntity;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  19. use Symfony\Component\HttpFoundation\Session\Session;
  20. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  21. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  22. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  23. use Shopware\Core\Framework\Struct\ArrayEntity;
  24. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  25. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  26. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  27. use Shopware\Core\Content\Product\Events\ProductCrossSellingStreamCriteriaEvent;
  28. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  29. class ProductSubscriber implements EventSubscriberInterface
  30. {
  31. /**
  32. * @var ContainerInterface
  33. */
  34. protected $container;
  35. /**
  36. * @var Connection
  37. */
  38. private $connection;
  39. /**
  40. * @var RouterInterface
  41. */
  42. private $router;
  43. /**
  44. * @var RequestStack
  45. */
  46. private $requestStack;
  47. /** @var ResponseEvent */
  48. private $event;
  49. /**
  50. * @var EntityRepositoryInterface
  51. */
  52. private $productRepository;
  53. /**
  54. * @var Session
  55. */
  56. private $session;
  57. /**
  58. * @var EntityRepositoryInterface
  59. */
  60. private $categoryRepository;
  61. public function __construct(
  62. ContainerInterface $container,
  63. Connection $connection,
  64. RouterInterface $router,
  65. RequestStack $requestStack,
  66. EntityRepositoryInterface $productRepository,
  67. Session $session,
  68. EntityRepositoryInterface $categoryRepository
  69. ) {
  70. $this->container = $container;
  71. $this->connection = $connection;
  72. $this->router = $router;
  73. $this->productRepository = $productRepository;
  74. $this->requestStack = $requestStack;
  75. $this->session = $session;
  76. $this->categoryRepository = $categoryRepository;
  77. }
  78. public static function getSubscribedEvents(): array
  79. {
  80. return [
  81. ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded',
  82. ProductEvents::PRODUCT_LISTING_CRITERIA => 'productListingCriteria',
  83. ProductEvents::PRODUCT_SEARCH_CRITERIA => 'productSearchCriteria',
  84. ProductEvents::PRODUCT_SUGGEST_CRITERIA => 'productSuggestCriteria',
  85. ProductPageCriteriaEvent::class => 'productPageCriteria',
  86. ProductCrossSellingStreamCriteriaEvent::class => 'productCrossSellingStreamCriteria',
  87. ProductCrossSellingIdsCriteriaEvent::class => 'productCrossSellingIdsCriteria'
  88. ];
  89. }
  90. public function onProductLoaded(EntityLoadedEvent $event): void
  91. {
  92. $customerLoggedIn = $this->session->get('customerLoggedIn');
  93. $masterRequest = $this->requestStack->getMasterRequest();
  94. if(empty($masterRequest)) {
  95. return;
  96. }
  97. $isSalesChannel = $masterRequest->get('_is_sales_channel');
  98. if(empty($isSalesChannel)) {
  99. return;
  100. }
  101. /** @var ProductEntity $product */
  102. foreach ($event->getEntities() as $product) {
  103. $showProductPrice = new ArrayEntity(['showProductPrice' => true]);
  104. if($customerLoggedIn)
  105. {
  106. $showProductPrice = new ArrayEntity(['showProductPrice' => true]);
  107. } else {
  108. if(!empty($product->getParentId()))
  109. {
  110. $productDetails = $this->getProductDetailsById($product->getParentId());
  111. $categoryTreeIds = $productDetails->getCategoryTree();
  112. } else {
  113. $categoryTreeIds = $product->getCategoryTree();
  114. }
  115. if(!empty($categoryTreeIds))
  116. {
  117. $categoryTree = $this->loadCategoriesTree($categoryTreeIds);
  118. $showProductPriceStatus = $this->checkShowPriceOrNotFromTree($categoryTree);
  119. $showProductPrice = new ArrayEntity(['showProductPrice' => $showProductPriceStatus]);
  120. }
  121. }
  122. $product->addExtension("showProductPrice", $showProductPrice);
  123. }
  124. }
  125. private function loadCategoriesTree(array $ids)
  126. {
  127. if(empty($ids)) return null;
  128. $criteria = new Criteria();
  129. $criteria->addFilter(new EqualsAnyFilter('id', $ids));
  130. $criteria->addAssociation('customFields');
  131. $criteria->addSorting(new FieldSorting('level'));
  132. $categoryTree = $this->categoryRepository->search($criteria, Context::createDefaultContext())->getEntities();
  133. return $categoryTree;
  134. }
  135. public function getProductDetailsById($id)
  136. {
  137. $criteria = (new Criteria())
  138. ->addFilter(new EqualsFilter(
  139. 'product.id', $id));
  140. $criteria->addAssociation('categories');
  141. $criteria->addAssociation('categories.customFields');
  142. $result = $this->productRepository->search($criteria, Context::createDefaultContext());
  143. $productDetails = $result->first();
  144. return ($productDetails) ? $productDetails: null;
  145. }
  146. public function checkShowPriceOrNotFromTree($categoryTree)
  147. {
  148. foreach($categoryTree as $category)
  149. {
  150. $customFields = $category->getCustomFields();
  151. if($customFields)
  152. {
  153. if($customFields['implify_show_price_after_login'] == true)
  154. {
  155. return false;
  156. }
  157. }
  158. }
  159. return true;
  160. }
  161. public function productSuggestCriteria(ProductSuggestCriteriaEvent $event)
  162. {
  163. $this->addCategoryAssociation($event);
  164. return true;
  165. }
  166. public function productSearchCriteria(ProductSearchCriteriaEvent $event)
  167. {
  168. $this->addCategoryAssociation($event);
  169. return true;
  170. }
  171. public function productListingCriteria(ProductListingCriteriaEvent $event)
  172. {
  173. $this->addCategoryAssociation($event);
  174. return true;
  175. }
  176. public function productPageCriteria(ProductPageCriteriaEvent $event)
  177. {
  178. $this->addCategoryAssociation($event);
  179. return true;
  180. }
  181. public function productCrossSellingStreamCriteria(ProductCrossSellingStreamCriteriaEvent $event)
  182. {
  183. $this->addCategoryAssociation($event);
  184. return true;
  185. }
  186. public function productCrossSellingIdsCriteria(ProductCrossSellingIdsCriteriaEvent $event)
  187. {
  188. $this->addCategoryAssociation($event);
  189. return true;
  190. }
  191. public function addCategoryAssociation($event)
  192. {
  193. $event->getCriteria()->addAssociation('categories');
  194. $event->getCriteria()->addAssociation('categories.customFields');
  195. return;
  196. }
  197. }