vendor/store.shopware.com/dreiscset/src/Subscriber/ProductEvents/ProductPageLoadedSubscriber.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSet\Subscriber\ProductEvents;
  3. use DreiscSet\Core\Content\DreiscSetProductExtension\DreiscSetProductExtensionEntity;
  4. use DreiscSet\Core\Content\Product\SalesChannelProductRepository;
  5. use DreiscSet\Core\Set\AbstractSetLoaderService;
  6. use DreiscSet\Core\SetOrder\AbstractSetOrderService;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductPageLoadedSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var AbstractSetLoaderService
  15.      */
  16.     protected $setLoaderService;
  17.     /**
  18.      * @var AbstractSetOrderService
  19.      */
  20.     protected $setOrderService;
  21.     /**
  22.      * @var SalesChannelProductRepository
  23.      */
  24.     protected $salesChannelProductRepository;
  25.     /**
  26.      * @param AbstractSetLoaderService $setLoaderService
  27.      * @param AbstractSetOrderService $setOrderService
  28.      * @param SalesChannelProductRepository $salesChannelProductRepository
  29.      */
  30.     public function __construct(AbstractSetLoaderService $setLoaderServiceAbstractSetOrderService $setOrderServiceSalesChannelProductRepository $salesChannelProductRepository)
  31.     {
  32.         $this->setLoaderService $setLoaderService;
  33.         $this->setOrderService $setOrderService;
  34.         $this->salesChannelProductRepository $salesChannelProductRepository;
  35.     }
  36.     /**
  37.      * @return array|void
  38.      */
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  43.         ];
  44.     }
  45.     public function onProductPageLoaded(ProductPageLoadedEvent $productPageLoadedEvent)
  46.     {
  47.         $productEntity $productPageLoadedEvent->getPage()->getProduct();
  48.         $salesChannelContext $productPageLoadedEvent->getSalesChannelContext();
  49.         $dreiscSetOrderId $productPageLoadedEvent->getRequest()->get('dreiscSetOrderId');
  50.         $showSetParameterIsSetOrSetOrderIdWasLoad = !empty($productPageLoadedEvent->getRequest()->get('showSet')) || !empty($dreiscSetOrderId);
  51.         /** Fetch the product extension dreiscSetProductExtension */
  52.         /** @var DreiscSetProductExtensionEntity $dreiscSetProductExtension */
  53.         $dreiscSetProductExtension $productEntity->getExtension('dreiscSetProductExtension');
  54.         /** Load the parent in case of variant */
  55.         if (null === $dreiscSetProductExtension && null !== $productEntity->getParentId()) {
  56.             $parentProductEntity $this->salesChannelProductRepository->get(
  57.                 $productEntity->getParentId(),
  58.                 new Criteria(),
  59.                 $productPageLoadedEvent->getSalesChannelContext()
  60.             );
  61.             $dreiscSetProductExtension $parentProductEntity->getExtension('dreiscSetProductExtension');
  62.         }
  63.         /** Abort, if it is not a set product */
  64.         if (null === $dreiscSetProductExtension) {
  65.             return;
  66.         }
  67.         /** Abort, if closeout and stock is empty */
  68.         if ($productEntity->getStock() <= && $productEntity->getIsCloseout()) {
  69.             return;
  70.         }
  71.         /** Abort, if dreisc set id is null */
  72.         if (null === $dreiscSetProductExtension->getDreiscSetId()) {
  73.             return;
  74.         }
  75.         /** Load the set order */
  76.         $setOrderCartStruct $this->setOrderService->loadSetOrder(
  77.             $dreiscSetProductExtension->getDreiscSetId(),
  78.             $salesChannelContext,
  79.             $dreiscSetOrderId,
  80.             $productEntity->getId(),
  81.             true
  82.         );
  83.         if (null === $setOrderCartStruct) {
  84.             return;
  85.         }
  86.         /** Load the set configuration struct */
  87.         $setStruct $this->setLoaderService->loadSet($setOrderCartStruct->getDreiscSetId(), $salesChannelContext);
  88.         /** Abortion if the product cannot only be bought as a set and the set GET parameter is not set. */
  89.         if (!$setStruct->isProductSetOnly() && false === $showSetParameterIsSetOrSetOrderIdWasLoad) {
  90.             $productEntity->addExtension(
  91.                 'dreiscSetIsAvailable',
  92.                 new ArrayStruct([
  93.                     'showBuyProductInSetButton' => true
  94.                 ])
  95.             );
  96.             /** Abort */
  97.             return;
  98.         }
  99.         /** Assign the set configuration struct */
  100.         $productEntity->addExtension(
  101.             'dreiscSetStruct',
  102.             $setStruct
  103.         );
  104.         /** Load and assign the set order struct */
  105.         $productEntity->addExtension(
  106.             'dreiscSetOrderCartStruct',
  107.             $setOrderCartStruct
  108.         );
  109.     }
  110. }