custom/plugins/CustomFieldAdd/src/Subscriber/Category.php line 66

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CustomFieldAdd\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Struct\ArrayEntity;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  9. use Shopware\Core\Checkout\Customer\CustomerEntity;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Event\StorefrontRenderEvent;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Shopware\Core\Framework\Context;
  14. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  15. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  16. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  19. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  22. use Shopware\Core\Checkout\Cart\LineItemFactoryRegistry;
  23. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  24. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  25. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  26. use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilderInterface;
  27. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader;
  28. use Shopware\Core\Content\Product\SalesChannel\ProductCloseoutFilter;
  29. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  30. class Category implements EventSubscriberInterface
  31. {
  32.     private $categoryRepository;
  33.     private $systemConfigService;
  34.     private $connection;
  35.     private $productRepository;
  36.     private $requestStack;
  37.     private $factory;
  38.     public function __construct(
  39.         EntityRepositoryInterface     $categoryRepository,
  40.         SystemConfigService           $systemConfigService,
  41.         Connection                    $connection,
  42.         SalesChannelRepository        $productRepository,
  43. //        EntityRepository $productRepository,
  44.         EntityRepository              $productStreamRepository,
  45.         RequestStack                  $requestStack,
  46.         LineItemFactoryRegistry       $factory,
  47.         CartService                   $cartService,
  48.         ProductStreamBuilderInterface $productStreamBuilder,
  49.         ProductListingLoader          $listingLoader
  50. //        ProductCloseoutFilter $productCloseoutFilter
  51.     )
  52.     {
  53.         $this->categoryRepository $categoryRepository;
  54.         $this->systemConfigService $systemConfigService;
  55.         $this->connection $connection;
  56.         $this->productRepository $productRepository;
  57.         $this->productStreamRepository $productStreamRepository;
  58.         $this->requestStack $requestStack;
  59.         $this->factory $factory;
  60.         $this->cartService $cartService;
  61.         $this->productStreamBuilder $productStreamBuilder;
  62.         $this->listingLoader $listingLoader;
  63. //        $this->productCloseoutFilter = $productCloseoutFilter;
  64.     }
  65.     public static function getSubscribedEvents(): array
  66.     {
  67.         return [
  68.             ProductPageLoadedEvent::class => "onProductPageLoadedEvent",
  69.         ];
  70.     }
  71.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event)
  72.     {
  73.         $streamProductids = [];
  74.         $aProductsLoad = [];
  75.         $pArray = [];
  76.         $page $event->getPage();
  77.         $context $event->getContext();
  78.         if ($event->getPage()->getProduct()->getCustomFields()) {
  79.             if (isset($event->getPage()->getProduct()->getCustomFields()['product_custom_accessories_group'])) {
  80.                 $sProductStrem = [];
  81.                 $accessories_group $event->getPage()->getProduct()->getCustomFields()['product_custom_accessories_group'];
  82.                 foreach ($accessories_group as $streamid) {
  83.                     try {
  84.                         $filters $this->productStreamBuilder->buildFilters(
  85.                             $streamid,
  86.                             $event->getContext()
  87.                         );
  88.                         $criteria = new Criteria();
  89.                         $criteria->addFilter(...$filters);
  90.                         $criteria $this->handleAvailableStock($criteria$event->getSalesChannelContext());
  91.                         $searchResult $this->listingLoader->load($criteria$event->getSalesChannelContext());
  92.                     } catch (\Exception $e) {
  93.                         continue;
  94.                     }
  95.                     $products $searchResult->getEntities();
  96.                     $productStreamIdsFind = [];
  97.                     foreach ($products->getElements() as $k => $product) {
  98.                         $streamProductids[] = $product->getId();
  99.                     }
  100.                     $streamProducts[] = $products->getElements();
  101.                 }
  102.                 //~ $event->getPage()->addExtension('accessoriesProductsLoadStream', new ArrayEntity(['accessories_product_stream'=> $streamProducts]));
  103.             }
  104.             if (isset($event->getPage()->getProduct()->getCustomFields()['product_custom_accessories'])) {
  105.                 $custom_accessories_product $event->getPage()->getProduct()->getCustomFields()['product_custom_accessories'];
  106.                 $aProductsLoad $event->getPage()->getProduct()->getCustomFields()['product_custom_accessories'];
  107.             }
  108.             $stremFinal array_unique($streamProductids);
  109.             $aProductsLoad array_merge($aProductsLoad$stremFinal);
  110.             $aProductsLoad array_unique($aProductsLoad);
  111.             foreach ($aProductsLoad as $key => $val) {
  112.                 $sproductId $val;
  113.                 if (in_array($val, [$event->getPage()->getProduct()->getId()])) {
  114.                     continue;
  115.                 }
  116.                 $sCriteria = new Criteria();
  117.                 $sCriteria->addFilter(new EqualsFilter('id'$val));
  118.                 $sCriteria->addAssociation('cover');
  119.                 $sProductDatas $this->productRepository->search($sCriteria$event->getSalesChannelContext())->first();
  120. //                $sProductDatas= $this->productRepository->search($sCriteria, $event->getContext())->first();
  121.                 $bundleQuantity 0;
  122.                 if (empty($sProductDatas)) {
  123.                     continue;
  124.                 }
  125.                 if (!$sProductDatas->getActive()) {
  126.                     continue;
  127.                 }
  128.                 if ($sProductDatas->getChildCount()) {
  129.                     continue;
  130.                 }
  131.                 if ($sProductDatas->getIsCloseout()) {
  132.                     continue;
  133.                 }
  134.                 $pArray[] = $sProductDatas;
  135.             }
  136.             if ($pArray) {
  137.                 $event->getPage()->addExtension('accessoriesProductsLoad', new ArrayEntity(['accessories_product' => $pArray]));
  138.             }
  139.             $categoryIds $page->getProduct()->getCategoryIds();
  140.             foreach ($categoryIds as $categoryId) {
  141.                 $criteria = new Criteria([$categoryId]);
  142.                 $criteria->addAssociation('customFields');
  143.                 $category $this->categoryRepository->search($criteria$event->getContext())->first();
  144.                 $categoryCustomFields $category->getCustomFields();
  145.                 if (isset($categoryCustomFields['custom_spp_category_product'])) {
  146.                     $page->addExtension('categoryCustomFields', new ArrayEntity($categoryCustomFields));
  147.                     break;
  148.                 }
  149.             }
  150.         }
  151.     }
  152.     private function handleAvailableStock(Criteria $criteriaSalesChannelContext $context): Criteria
  153.     {
  154.         $salesChannelId $context->getSalesChannel()->getId();
  155.         $hide $this->systemConfigService->get('core.listing.hideCloseoutProductsWhenOutOfStock'$salesChannelId);
  156.         if (!$hide) {
  157.             return $criteria;
  158.         }
  159.         $criteria->addFilter(new ProductCloseoutFilter());
  160.         return $criteria;
  161.     }
  162. }