vendor/store.shopware.com/acrisproductbadgescs/src/Storefront/Subscriber/ProductBadgeSubscriber.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ProductBadges\Storefront\Subscriber;
  3. use Acris\ProductBadges\Components\ProductBadges\ProductBadgesService;
  4. use Acris\ProductBadges\Components\ProductBadges\Struct\BadgeLoadingAllowedStruct;
  5. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  7. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  8. use Shopware\Core\System\SalesChannel\Event\SalesChannelProcessCriteriaEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ProductBadgeSubscriber implements EventSubscriberInterface
  11. {
  12.     public const ACRIS_STREAM_IDS_EXTENSION 'acrisStreamIds';
  13.     public const CRITERIA_TITLE_BLACKLIST = [
  14.         'cart::products'
  15.     ];
  16.     private ProductBadgesService $productBadgeService;
  17.     public function __construct(ProductBadgesService $productBadgeService)
  18.     {
  19.         $this->productBadgeService $productBadgeService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             'sales_channel.product.process.criteria' => 'onProductCriteriaLoaded',
  25.             'sales_channel.product.loaded' => ['onProductLoaded', -60]
  26.         ];
  27.     }
  28.     public function onProductCriteriaLoaded(SalesChannelProcessCriteriaEvent $event): void
  29.     {
  30.         $criteria $event->getCriteria();
  31.         if(!empty($criteria->getTitle()) && in_array($criteria->getTitle(), self::CRITERIA_TITLE_BLACKLIST)) {
  32.             $event->getContext()->addExtension(BadgeLoadingAllowedStruct::EXTENSION_KEY, new BadgeLoadingAllowedStruct(false));
  33.             return;
  34.         } else {
  35.             $event->getContext()->addExtension(BadgeLoadingAllowedStruct::EXTENSION_KEY, new BadgeLoadingAllowedStruct(true));
  36.         }
  37.         if($criteria->hasAssociation('streams') === false) {
  38.             $criteria->addAssociation('streams');
  39.         }
  40.     }
  41.     public function onProductLoaded(SalesChannelEntityLoadedEvent $event): void
  42.     {
  43.         if($event->getContext()->hasExtension(BadgeLoadingAllowedStruct::EXTENSION_KEY) && $event->getContext()->getExtension(BadgeLoadingAllowedStruct::EXTENSION_KEY)->isAllowed() !== true) {
  44.             return;
  45.         }
  46.         $productCollection = new EntityCollection();
  47.         foreach ($event->getEntities() as $product) {
  48.             if ($product instanceof SalesChannelProductEntity) {
  49.                 $productCollection->add($product);
  50.             }
  51.         }
  52.         $this->productBadgeService->addBadgesToProducts($productCollection$event->getSalesChannelContext());
  53.     }
  54. }