vendor/crehler/mojebambino-engine/src/Subscriber/ProductSubscriber.php line 81

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\MojeBambinoEngine\Service\PageLinksService;
  4. use Crehler\MojeBambinoEngine\Service\ProductStatusStorefrontService;
  5. use Shopware\Core\Content\Category\CategoryEntity;
  6. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  7. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  15. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  18. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class ProductSubscriber implements EventSubscriberInterface
  21. {
  22.     private SalesChannelRepository $productRepository;
  23.     private EntityRepository $categoryRepository;
  24.     protected ProductStatusStorefrontService $productStatus;
  25.     protected PageLinksService $pageLinks;
  26.     public function __construct(
  27.         SalesChannelRepository         $productRepository,
  28.         EntityRepository               $categoryRepository,
  29.         ProductStatusStorefrontService $productStatus,
  30.         PageLinksService               $pageLinks
  31.     )
  32.     {
  33.         $this->productRepository $productRepository;
  34.         $this->categoryRepository $categoryRepository;
  35.         $this->productStatus $productStatus;
  36.         $this->pageLinks $pageLinks;
  37.     }
  38.     /**
  39.      * @return array
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44. //            ProductLoaderCriteriaEvent::class => 'onProductLoaderCriteria',
  45.             ProductPageCriteriaEvent::class => 'onProductLoaderCriteria',
  46.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  47.         ];
  48.     }
  49.     /**
  50.      * @param ProductPageCriteriaEvent $event
  51.      */
  52.     //public function onProductLoaderCriteria(ProductLoaderCriteriaEvent $event): void
  53.     public function onProductLoaderCriteria(ProductPageCriteriaEvent $event): void
  54.     {
  55.         try {
  56.             $event
  57.                 ->getCriteria()
  58.                 ->addAssociation('pageLinks')
  59.                 ->addAssociation('alternativeProducts')
  60.                 ->addAssociation('checkoutCrossSellingStream')
  61.                 ->addAssociation('crProductStatus')
  62.                 ->addAssociation('crProductStatusId')
  63.                 ->getAssociation('visibilities')
  64.                 ->addFilter(new EqualsFilter('salesChannelId'$event->getSalesChannelContext()->getSalesChannel()->getId()));
  65.         } catch (\Throwable $exception) {
  66.             dd($exception);
  67.         }
  68.     }
  69.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  70.     {
  71.         $product $event->getPage()->getProduct();
  72.         if ($event->getRequest()->get("c")) {
  73.             $criteria = (new Criteria())->addFilter(new EqualsFilter('autoIncrement'$event->getRequest()->get("c")));
  74.             /** @var CategoryEntity $category */
  75.             $category $this->categoryRepository->search($criteriaContext::createDefaultContext())->first();
  76.             $event->getPage()->getProduct()->addExtension('setBreadcrumb'$category);
  77.         }
  78.         $alternativeProducts $product->getExtension('alternativeProducts');
  79.         if ($alternativeProducts != null) { /* ------------- dodaƂam warunek ------ */
  80.             $alternativeProductsIds array_keys($alternativeProducts->getElements());
  81.         }
  82.         if (!empty($alternativeProductsIds)) {
  83.             $product->addExtension('alternativeProductReal'$this->getProductAlternative(
  84.                 $alternativeProductsIds,
  85.                 $event->getSalesChannelContext()
  86.             ));
  87.         }
  88.         $product->addExtension('productStatus'$this->productStatus->getProductStatus($product$event->getContext()));
  89.         $product->addExtension('pageLinks'$this->pageLinks->getProductPageLinks($product$event->getContext()));
  90.         $categoryTree $product->getCategoryTree();
  91.         $cat_mb_id $cat_ze_sciezki null;
  92.         $referer $event->getRequest()->headers->get('referer');
  93.         if ($referer != "") {
  94.             $ref_tab explode("/"$referer);
  95.             /* echo "<pre>";
  96.              print_r($ref_tab);
  97.              print_r($product->getCategoryTree());
  98.              echo "</pre>"; */
  99.             if (count($ref_tab) == 5) {
  100.                 $cat_mb_id = (int)$ref_tab[3];
  101.                 //$a = $this->getCategoryByAutoIncr($cat_mb_id, $event->getContext());
  102.                 //$criteria = new Criteria(['f92569b5dd4247bb94c6d412da11a6e9', '840dcda202934addb055b091937afa20']);
  103.                 $criteria = new Criteria($product->getCategoryTree());
  104.                 $wynik $this->categoryRepository->search($criteria$event->getContext());
  105.                 foreach ($wynik as $wyn) {
  106.                     //echo "<pre>"; print_r($wyn->getAutoIncrement()); echo "</pre>";
  107.                     if ($wyn->getAutoIncrement() == $cat_mb_id) {
  108.                         $cat_ze_sciezki $wyn->getId();
  109.                         break;
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.         $catId null;
  115.         if ($product->getMainCategories() != null) {
  116.             $mainCategories $product->getMainCategories()->getElements();
  117.         }
  118.         //echo "<pre>"; print_r($mainCategories); echo "</pre>";
  119.         if (!empty($mainCategories)) {
  120.             foreach ($mainCategories as $cat) {
  121.                 //echo "<pre>"; print_r($cat->getCategoryId()); echo "</pre>";
  122.                 $catId $cat->getCategoryId();
  123.             }
  124.         }
  125.         $treeCategoryId null;
  126.         if (is_array($categoryTree)) {
  127.             $treeCategoryId end($categoryTree);
  128.         }
  129.         if ($cat_ze_sciezki) {
  130.             $product->addExtension('categoryBreadcrumb'$this->getCategory($cat_ze_sciezki$event->getContext()));
  131.         } elseif ($catId) {
  132.             $product->addExtension('categoryBreadcrumb'$this->getCategory($catId$event->getContext()));
  133.         } else {
  134.             if ($treeCategoryId) {
  135.                 $product->addExtension('categoryBreadcrumb'$this->getCategory($treeCategoryId$event->getContext()));
  136.             } else {
  137.                 $product->addExtension('categoryBreadcrumb'null);
  138.             }
  139.         }
  140.         if ($product->getVisibilities() != null) {
  141.             if ($product->getVisibilities()->first()
  142.                 && $product->getVisibilities()->first()->getVisibility() === ProductVisibilityDefinition::VISIBILITY_LINK) {
  143.                 $product->setAvailable(false);
  144.             }
  145.         }
  146.     }
  147.     private function getProductAlternative(array $productIdsSalesChannelContext $context): EntityCollection
  148.     {
  149.         $criteria = new Criteria($productIds);
  150.         $criteria->addFilter(new ProductAvailableFilter($context->getSalesChannel()->getId()));
  151.         return $this->productRepository->search($criteria$context)->getEntities();
  152.     }
  153.     private function getCategory(string $categoryIdContext $context): ?CategoryEntity
  154.     {
  155.         $criteria = new Criteria([$categoryId]);
  156.         return $this->categoryRepository->search($criteria$context)->get($categoryId);
  157.     }
  158. }