vendor/crehler/size-table/Subscriber/ProductLoaderSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   12 maj 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\SizeTable\Subscriber;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  16. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class ProductLoaderSubscriber implements EventSubscriberInterface
  19. {
  20.     private EntityRepositoryInterface $productRepository;
  21.     public function __construct(EntityRepositoryInterface $productRepository)
  22.     {
  23.         $this->productRepository $productRepository;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             ProductPageCriteriaEvent::class => 'onProductCriteria',
  29.             ProductPageLoadedEvent::class => 'onProductsLoaded',
  30.         ];
  31.     }
  32.     public function onProductCriteria(ProductPageCriteriaEvent $event)
  33.     {
  34.         try {
  35.             $event
  36.                 ->getCriteria()
  37.                 ->addAssociation('sizeTables');
  38.         } catch (\Exception $e) {
  39.         }
  40.     }
  41.     public function onProductsLoaded(ProductPageLoadedEvent $event)
  42.     {
  43.         $product $event->getPage()->getProduct();
  44.         if ($product->getParentId() && ($product->getExtension('sizeTables') === null || $product->getExtension('sizeTables'))) {
  45.             $criteria = new Criteria([$product->getParentId()]);
  46.             $criteria->addAssociation('sizeTables');
  47.             $parent $this->productRepository->search($criteria$event->getContext())->first();
  48.             $product->addExtension('sizeTables'$parent->getExtension('sizeTables'));
  49.         }
  50.     }
  51. }