custom/plugins/b2bsellerscore/addons/B2bOffer/Subscriber/ProductPageSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace B2bOffer\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class ProductPageSubscriber implements EventSubscriberInterface
  16. {
  17.     use B2bContextTrait;
  18.     private SystemConfigService $configService;
  19.     private EntityRepositoryInterface $offerRepository;
  20.     public function __construct(
  21.         SystemConfigService       $configService,
  22.         EntityRepositoryInterface $offerRepository
  23.     )
  24.     {
  25.         $this->configService $configService;
  26.         $this->offerRepository $offerRepository;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductPageLoadedEvent::class => 'addOpenOfferData',
  32.         ];
  33.     }
  34.     public function addOpenOfferData(ProductPageLoadedEvent $event): void
  35.     {
  36.         if ($event->getSalesChannelContext()->getCustomer()) {
  37.             $event->getPage()->addExtension('B2bOpenOffers'$this->getOffers($event->getSalesChannelContext(), $event->getContext()));
  38.         }
  39.     }
  40.     private function getOffers(SalesChannelContext $salesChannelContextContext $context): EntityCollection
  41.     {
  42.         $criteria = new Criteria();
  43.         $criteria->addFilter(new EqualsFilter('salesChannelId'$salesChannelContext->getSalesChannelId()));
  44.         $criteria->addFilter(new EqualsFilter('offerCustomer.customerId'$salesChannelContext->getCustomer()->getId()));
  45.         $criteria->addAssociation('offerCustomer');
  46.         $criteria->addAssociation('status');
  47.         $criteria->addAssociation('employee');
  48.         $config $this->getPluginConfig();
  49.         $statusIdsWhereProductsCanBeAdded $config['statusIdsWhereProductsCanBeAdded'] ?? [$config['defaultCartStatusId']];
  50.         $criteria->addFilter(new EqualsAnyFilter('statusId'$statusIdsWhereProductsCanBeAdded));
  51.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::DESCENDING));
  52.         $criteria->setLimit('10');
  53.         return $this->offerRepository->search($criteria$context)->getEntities();
  54.     }
  55.     public function getPluginConfig()
  56.     {
  57.         return $this->configService->get('B2bOffer.config');
  58.     }
  59. }