custom/plugins/VioCustomerPrice/src/Subscriber/ProductCriteriaEventSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioCustomerPrice\Subscriber;
  4. use Shopware\Core\Content\Product\Events\ProductCrossSellingCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductCrossSellingStreamCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductGatewayCriteriaEvent;
  7. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  8. use Shopware\Core\Content\Product\Events\ProductListingPreviewCriteriaEvent;
  9. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  10. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  11. use Shopware\Core\Content\Product\ProductEvents;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  17. use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageCriteriaEvent;
  18. use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
  19. use Shopware\Storefront\Pagelet\Wishlist\GuestWishListPageletProductCriteriaEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class ProductCriteriaEventSubscriber implements EventSubscriberInterface
  22. {
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ProductGatewayCriteriaEvent::class => 'onProductGatewayCriteria',
  27.             ProductCrossSellingCriteriaEvent::class => 'onProductCrossSellingCriteriaEvent',
  28.             ProductCrossSellingStreamCriteriaEvent::class => 'onProductCrossSellingCriteriaEvent',
  29.             ProductListingCriteriaEvent::class => 'onProductListingCriteriaEvent',
  30.             ProductListingPreviewCriteriaEvent::class => 'onProductListingPreviewCriteriaEvent',
  31.             ProductSearchCriteriaEvent::class => 'onProductListingCriteriaEvent',
  32.             ProductSuggestCriteriaEvent::class => 'onProductListingCriteriaEvent',
  33.             ProductPageCriteriaEvent::class => 'onProductPageCriteriaEvent',
  34.             WishListPageProductCriteriaEvent::class => 'onWishListPageProductCriteriaEvent',
  35.             GuestWishListPageletProductCriteriaEvent::class => 'onGuestWishListPageletProductCriteriaEvent',
  36.             MinimalQuickViewPageCriteriaEvent::class => 'onMinimalQuickViewPageCriteriaEvent',
  37.         ];
  38.     }
  39.     public function onMinimalQuickViewPageCriteriaEvent(MinimalQuickViewPageCriteriaEvent $event): void
  40.     {
  41.         $this->addCustomerPricesAssociation(
  42.             $event->getCriteria(),
  43.             $event->getSalesChannelContext()
  44.         );
  45.     }
  46.     public function onGuestWishListPageletProductCriteriaEvent(GuestWishListPageletProductCriteriaEvent $event): void
  47.     {
  48.         $this->addCustomerPricesAssociation(
  49.             $event->getCriteria(),
  50.             $event->getSalesChannelContext()
  51.         );
  52.     }
  53.     public function onWishListPageProductCriteriaEvent(WishListPageProductCriteriaEvent $event): void
  54.     {
  55.         $this->addCustomerPricesAssociation(
  56.             $event->getCriteria(),
  57.             $event->getSalesChannelContext()
  58.         );
  59.     }
  60.     public function onProductPageCriteriaEvent(ProductPageCriteriaEvent $event): void
  61.     {
  62.         $this->addCustomerPricesAssociation(
  63.             $event->getCriteria(),
  64.             $event->getSalesChannelContext()
  65.         );
  66.     }
  67.     public function onProductListingPreviewCriteriaEvent(ProductListingPreviewCriteriaEvent $event): void
  68.     {
  69.         $this->addCustomerPricesAssociation(
  70.             $event->getCriteria(),
  71.             $event->getSalesChannelContext()
  72.         );
  73.     }
  74.     public function onProductCrossSellingCriteriaEvent(ProductCrossSellingCriteriaEvent $event): void
  75.     {
  76.         $this->addCustomerPricesAssociation(
  77.             $event->getCriteria(),
  78.             $event->getSalesChannelContext()
  79.         );
  80.     }
  81.     public function onProductListingCriteriaEvent(ProductListingCriteriaEvent $event): void
  82.     {
  83.         $this->addCustomerPricesAssociation(
  84.             $event->getCriteria(),
  85.             $event->getSalesChannelContext()
  86.         );
  87.     }
  88.     public function onProductGatewayCriteria(ProductGatewayCriteriaEvent $event): void
  89.     {
  90.         $this->addCustomerPricesAssociation(
  91.             $event->getCriteria(),
  92.             $event->getSalesChannelContext()
  93.         );
  94.     }
  95.     protected function addCustomerPricesAssociation(Criteria $criteriaSalesChannelContext $context): void
  96.     {
  97.         if($context->getCustomer()) {
  98.             $criteria->getAssociation('customerPrices')
  99.                 ->addFilter(new EqualsFilter('customerId'$context->getCustomer()->getId()))
  100.                 ->addSorting(new FieldSorting('quantityStart'));
  101.         }
  102.     }
  103. }