custom/plugins/MadArticleCustomergroup/src/Subscriber/ProductSubscriber.php line 138

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadArticleCustomergroup\Subscriber;
  4. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  5. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  6. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  12. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  13. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  14. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  15. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  16. use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  24. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
  25. use Bodymed\Webshop\StoreFront\Service\MadArticleCustomerGroupCheckData;
  26. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  27. use Shopware\Core\Content\Product\ProductEvents;
  28. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  29. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  30. use Shopware\Core\Framework\Struct\ArrayEntity;
  31. use Bodymed\Webshop\Core\Content\Product\SalesChannel\Price\MadArticleVariantsListingPrice;
  32. class ProductSubscriber  implements EventSubscriberInterface
  33. {
  34.     /**
  35.      * @var EntityRepositoryInterface
  36.      */
  37.     private EntityRepositoryInterface $customerGroupRepository;
  38.     private EntityRepositoryInterface $tagRepository;
  39.     private MadArticleCustomerGroupCheckData $checkData;
  40.     private MadArticleVariantsListingPrice $MadArticleVariantsListingPrice;
  41.     private SystemConfigService $systemConfigService;
  42.     public function __construct(
  43.         EntityRepositoryInterface $customerGroupRepository,
  44.         EntityRepositoryInterface $tagRepository,
  45.         MadArticleCustomerGroupCheckData $checkData,
  46.         MadArticleVariantsListingPrice $MadArticleVariantsListingPrice,
  47.         SystemConfigService $systemConfigService
  48.     )
  49.     {
  50.         $this->customerGroupRepository $customerGroupRepository;
  51.         $this->tagRepository $tagRepository;
  52.         $this->checkData $checkData;
  53.         $this->MadArticleVariantsListingPrice $MadArticleVariantsListingPrice;
  54.         $this->systemConfigService $systemConfigService;
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  60.             ProductListingResultEvent::class => 'onProductListingResult',
  61.             ProductSearchResultEvent::class => 'onProductListingResult',
  62.             ProductCrossSellingsLoadedEvent::class => 'onProductCrossSellingResult',
  63.             ProductSuggestCriteriaEvent::class => 'onProductSuggestCriteriaEvent',
  64.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteriaEvent',
  65.             ProductListingCriteriaEvent::class => 'onProductListingCriteriaEvent',
  66.             ProductCrossSellingIdsCriteriaEvent::class => 'onProductCrossSellingIdsCriteriaEvent',
  67.             //ProductSuggestResultEvent::class => 'onProductListingResult',
  68.             //'sales_channel.product.loaded' => 'salesChannelProductLoaded',
  69.         ];
  70.     }
  71.     /* Product crossselling get individuell customer prices for Variants */
  72.     public function onProductCrossSellingResult(ProductCrossSellingsLoadedEvent $event): void
  73.     {
  74.         $salesChannelContext $event->getSalesChannelContext();
  75.         if($event->getCrossSellings()){
  76.             $crossSellings $event->getCrossSellings()->getElements();
  77.             foreach($crossSellings as $crossSelling){
  78.                 $products $crossSelling->getProducts()->getElements();
  79.                 foreach($products as $product){
  80.                     $variants $this->MadArticleVariantsListingPrice->getVariants($product$salesChannelContext);
  81.                     if($variants){
  82.                         $data = new ArrayEntity(['newListPrice' => $variants]);
  83.                         $product->addExtension('newListPrice'$data);
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     /* Product listing get individuell customer prices for Variants */
  90.     public function onProductListingResult(ProductListingResultEvent $event): void
  91.     {
  92.         $salesChannelContext $event->getSalesChannelContext();
  93.         $products $event->getResult()->getElements();
  94.         foreach($products as $product){
  95.             $variants $this->MadArticleVariantsListingPrice->getVariants($product$salesChannelContext);
  96.             if($variants){
  97.                 $data = new ArrayEntity(['newListPrice' => $variants]);
  98.                 $product->addExtension('newListPrice'$data);
  99.             }
  100.         }
  101.     }
  102.     /* Crosselling Stream */
  103.     public function onProductCrossSellingIdsCriteriaEvent(ProductCrossSellingIdsCriteriaEvent $event): void
  104.     {
  105.         $criteria $event->getCriteria();
  106.         $customer $event->getSalesChannelContext()->getCustomer();
  107.         $visibility ProductVisibilityDefinition::VISIBILITY_ALL;
  108.         $criteria $this->getCriteria($event->getSalesChannelContext()->getSalesChannel()->getId(), $criteria$customer$visibility);
  109.     }
  110.     /* Ajax Suche */
  111.     public function onProductSuggestCriteriaEvent(ProductSuggestCriteriaEvent $event): void
  112.     {
  113.         $criteria $event->getCriteria();
  114.         $customer $event->getSalesChannelContext()->getCustomer();
  115.         $visibility ProductVisibilityDefinition::VISIBILITY_SEARCH;
  116.         $criteria $this->getCriteria($event->getSalesChannelContext()->getSalesChannel()->getId(), $criteria$customer$visibility);
  117.     }
  118.     /* Suchergebnisse */
  119.     public function onProductSearchCriteriaEvent(ProductSearchCriteriaEvent $event): void
  120.     {
  121.         $criteria $event->getCriteria();
  122.         $customer $event->getSalesChannelContext()->getCustomer();
  123.         $visibility ProductVisibilityDefinition::VISIBILITY_SEARCH;
  124.         $criteria $this->getCriteria($event->getSalesChannelContext()->getSalesChannel()->getId(), $criteria$customer$visibility);
  125.     }
  126.     /* Listing */
  127.     public function onProductListingCriteriaEvent(ProductListingCriteriaEvent $event): void
  128.     {
  129.         $criteria $event->getCriteria();
  130.         $customer $event->getSalesChannelContext()->getCustomer();
  131.         $visibility ProductVisibilityDefinition::VISIBILITY_ALL;
  132.         $criteria $this->getCriteria($event->getSalesChannelContext()->getSalesChannel()->getId(), $criteria$customer$visibility);
  133.     }
  134.     public function getCriteria($id$criteria$customer$visibility){
  135.         $guestGroup $this->systemConfigService->getString('MadArticleCustomergroup.config.guestGroup');
  136.         /* Filter Customer Product Feature Visibility */
  137.         $filters = [];
  138.         if($customer !== null) {
  139.             if($customer->getGroupId() === null){
  140.                 $group $guestGroup;
  141.             }else{
  142.                 $group $customer->getGroupId();
  143.             }
  144.             if($customer->getId()){
  145.                 if ($customer->getTagIds() !== null) {
  146.                     foreach ($customer->getTagIds() as $tagId) {
  147.                         $filters[] = new ContainsFilter('product.customFields.mad_tags_enable'$tagId);
  148.                     }
  149.                 }
  150.             }
  151.             $criteria->addFilter(
  152.                     new MultiFilter(MultiFilter::CONNECTION_OR, [
  153.                         /* Wenn Customer Produkt Feature ist wahr wird der Artikel angezeigt, da es höher Wertiger ist als die anderen Prüfungen */
  154.                         new MultiFilter(MultiFilter::CONNECTION_AND, [
  155.                             new EqualsFilter('customerProductFeature.customerId'$customer->getId()),
  156.                             new EqualsFilter('customerProductFeature.visible'true)
  157.                         ]),
  158.                         new MultiFilter(MultiFilter::CONNECTION_AND, [
  159.                             new NotFilter(NotFilter::CONNECTION_AND, [
  160.                                 new EqualsFilter('customerProductFeature.customerId'$customer->getId()),
  161.                                 new EqualsFilter('customerProductFeature.visible'false),
  162.                             ]),
  163.                             new MultiFilter(MultiFilter::CONNECTION_OR, [
  164.                                 /* Prüfen ob der Tag gesetzt ist und das Produkt Feature nicht */
  165.                                 new MultiFilter(MultiFilter::CONNECTION_OR$filters),
  166.                                 /* Prüfen ob die Gruppe vorhanden ist */
  167.                                 new MultiFilter(MultiFilter::CONNECTION_AND, [
  168.                                     new MultiFilter(MultiFilter::CONNECTION_OR, [
  169.                                         new EqualsFilter('product.customFields.mad_tags_enable'Null),
  170.                                         new EqualsFilter('product.customFields.mad_tags_enable''[]'),
  171.                                     ]),
  172.                                     new MultiFilter(MultiFilter::CONNECTION_OR, [
  173.                                         new ContainsFilter('product.customFields.mad_usergroups_enable'$group),
  174.                                     ])
  175.                                 ]),
  176.                             ])
  177.                         ]),
  178.                         new MultiFilter(MultiFilter::CONNECTION_AND, [
  179.                             new NotFilter(NotFilter::CONNECTION_AND, [
  180.                                 new EqualsFilter('customerProductFeature.customerId'$customer->getId()),
  181.                                 new EqualsFilter('customerProductFeature.visible'false),
  182.                             ]),
  183.                             new MultiFilter(MultiFilter::CONNECTION_OR, [
  184.                                 new EqualsFilter('product.customFields.mad_tags_enable'Null),
  185.                                 new EqualsFilter('product.customFields.mad_tags_enable''[]'),
  186.                             ]),
  187.                             new MultiFilter(MultiFilter::CONNECTION_OR, [
  188.                                 new EqualsFilter('product.customFields.mad_usergroups_enable'Null),
  189.                                 new EqualsFilter('product.customFields.mad_usergroups_enable''[]'),
  190.                             ]),
  191.                             new ProductAvailableFilter($id$visibility)
  192.                         ]),
  193.                     ])
  194.             );
  195.         }else{ /* Kein Kunde */
  196.             $criteria->addFilter(
  197.                     new MultiFilter(MultiFilter::CONNECTION_OR, [
  198.                         new ContainsFilter('product.customFields.mad_usergroups_enable'$guestGroup),
  199.                         new MultiFilter(MultiFilter::CONNECTION_AND, [
  200.                             new MultiFilter(MultiFilter::CONNECTION_OR, [
  201.                                 new EqualsFilter('product.customFields.mad_tags_enable'Null),
  202.                                 new EqualsFilter('product.customFields.mad_tags_enable''[]'),
  203.                             ]),
  204.                             new MultiFilter(MultiFilter::CONNECTION_OR, [
  205.                                 new EqualsFilter('product.customFields.mad_usergroups_enable'Null),
  206.                                 new EqualsFilter('product.customFields.mad_usergroups_enable''[]'),
  207.                             ]),
  208.                             new ProductAvailableFilter($id$visibility)
  209.                         ]),
  210.                     ])
  211.                 );
  212.         }
  213.         return $criteria;
  214.     }
  215.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  216.     {
  217.         $salesChannelContext $event->getSalesChannelContext();
  218.         $product $event->getPage()->getProduct();
  219.         $product->MadShowArticle true;
  220.         $product->MadShowTagArticle true;
  221.         $guestGroup $this->systemConfigService->getString('MadArticleCustomergroup.config.guestGroup');
  222.        if(!$this->checkData->checkArticleVisibility($salesChannelContext->getCustomer(), $product $guestGroup)){
  223.            if($salesChannelContext->getCustomer() == null){
  224.                $product->MadLogin false;
  225.            }else{
  226.                $product->MadLogin true;
  227.            }
  228.            $product->MadShowArticle false;
  229.        }
  230.     }
  231. }