custom/plugins/TwigelSportB2b/src/Subscriber/FilterSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace Twigel\SportB2B\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  4. use Shopware\Core\Content\Product\ProductEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class FilterSubscriber implements EventSubscriberInterface
  16. {
  17.     private EntityRepositoryInterface $advancedPricesRepository;
  18.     private RequestStack $requestStack;
  19.     public function __construct(
  20.         EntityRepositoryInterface $advancedPricesRepository,
  21.         RequestStack              $requestStack
  22.     )
  23.     {
  24.         $this->advancedPricesRepository $advancedPricesRepository;
  25.         $this->requestStack $requestStack;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             ProductEvents::PRODUCT_LOADED_EVENT => 'getAdvancedPrices',
  31.             ProductEvents::PRODUCT_LISTING_CRITERIA => 'onLoad',
  32.         ];
  33.     }
  34.     public function onLoad(ProductListingCriteriaEvent $event)
  35.     {
  36.         $event->getCriteria()->addAssociations(['tags']);
  37.     }
  38.     public function getAdvancedPrices(EntityLoadedEvent $event)
  39.     {
  40.         $request $this->requestStack->getCurrentRequest()->attributes->get('sw-sales-channel-context');
  41.         if ($request !== null) {
  42.             $ifLogged $request->getCustomer();
  43.             if ($ifLogged !== null) {
  44.                 $context $event->getContext();
  45.                 $groupId $ifLogged->getGroupId();
  46.                 $products $event->getEntities();
  47.                 foreach ($products as $product) {
  48.                     $productId $product->get('id');
  49.                     $criteria = new Criteria();
  50.                     $criteria->addFilter
  51.                     (
  52.                         new MultiFilter(
  53.                             MultiFilter::CONNECTION_AND,
  54.                             [
  55.                                 new EqualsFilter('groupId'$groupId),
  56.                                 new EqualsFilter('productId'$productId),
  57.                             ]
  58.                         )
  59.                     );
  60.                     $searchResult $this->advancedPricesRepository->search($criteria$context)->first();
  61.                     if (!empty($searchResult)) {
  62.                         $grossPrice $searchResult->get('gross');
  63.                         $result['productId'] = $productId;
  64.                         $result['price'] = $grossPrice;
  65.                         $product->addArrayExtension('advancedPricing'$result);
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }