custom/plugins/SwagSocialShopping/src/EventListener/ProductPageEventListener.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace SwagSocialShopping\EventListener;
  9. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  14. use SwagSocialShopping\Component\Network\Pinterest;
  15. use SwagSocialShopping\Component\Struct\PinterestMetaInformationExtension;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProductPageEventListener implements EventSubscriberInterface
  18. {
  19.     private EntityRepositoryInterface $socialShoppingSalesChannelRepository;
  20.     public function __construct(EntityRepositoryInterface $socialShoppingSalesChannelRepository)
  21.     {
  22.         $this->socialShoppingSalesChannelRepository $socialShoppingSalesChannelRepository;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ProductPageLoadedEvent::class => 'addPinterestSnippets',
  28.         ];
  29.     }
  30.     public function addPinterestSnippets(ProductPageLoadedEvent $event): void
  31.     {
  32.         $salesChannel $event->getSalesChannelContext()->getSalesChannel();
  33.         $criteria = new Criteria();
  34.         $criteria
  35.             ->addAssociation('salesChannelDomain')
  36.             ->addAssociation('salesChannel')
  37.             ->addFilter(
  38.                 new EqualsFilter('salesChannelDomain.salesChannelId'$salesChannel->getId()),
  39.                 new EqualsFilter('salesChannel.active'true),
  40.                 new EqualsFilter('network'Pinterest::class)
  41.             );
  42.         $result $this->socialShoppingSalesChannelRepository->searchIds($criteria$event->getContext());
  43.         if ($result->getTotal() !== 1) {
  44.             return;
  45.         }
  46.         $pinterestMetaInformation = new PinterestMetaInformationExtension();
  47.         $pinterestMetaInformation->setIsPinterestSalesChannel(true);
  48.         $pinterestMetaInformation->setAvailability(
  49.             $this->getPinterestProductAvailability($event->getPage()->getProduct())
  50.         );
  51.         $pageMetaInformation $event->getPage()->getMetaInformation();
  52.         if ($pageMetaInformation === null) {
  53.             return;
  54.         }
  55.         $pageMetaInformation->addExtension('pinterest'$pinterestMetaInformation);
  56.     }
  57.     private function getPinterestProductAvailability(SalesChannelProductEntity $productEntity): string
  58.     {
  59.         if (!$productEntity->getIsCloseout()
  60.             || $productEntity->getAvailableStock() >= $productEntity->getMinPurchase()
  61.         ) {
  62.             return PinterestMetaInformationExtension::AVAILABILITY_IN_STOCK;
  63.         }
  64.         if ($productEntity->getAvailableStock() < $productEntity->getMinPurchase()
  65.             && $productEntity->getRestockTime() && !$productEntity->getIsCloseout()
  66.         ) {
  67.             return PinterestMetaInformationExtension::AVAILABILITY_BACKORDER;
  68.         }
  69.         return PinterestMetaInformationExtension::AVAILABILITY_OUT_OF_STOCK;
  70.     }
  71. }