custom/plugins/BartCPreemiumArticle/src/Subscriber/ShowPremiumProducts.php line 91

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
  4. use BartCPreemiumArticle\PremiumArticle\Core\Content\Premium\PremiumDefinition;
  5. use BartCPreemiumArticle\PremiumArticle\Repository\MinPriceRepositoryInterface;
  6. use BartCPreemiumArticle\PremiumArticle\Repository\TotalPriceRepositoryInterface;
  7. use BartCPreemiumArticle\PremiumArticle\Service\CheckIfVariantsAreAvailableInterface;
  8. use BartCPreemiumArticle\PremiumArticle\Service\FindPremiumProductsInCartInterface;
  9. use BartCPreemiumArticle\PremiumArticle\Service\PremiumAccessControl;
  10. use BartCPreemiumArticle\PremiumArticle\Service\RemoveProductsWithMaxMultiAddCounts;
  11. use Psr\Log\LoggerInterface;
  12. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  21. use Shopware\Core\Framework\Struct\ArrayEntity;
  22. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  23. use Shopware\Core\System\SystemConfig\SystemConfigService;
  24. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  25. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  26. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  27. use Symfony\Component\DependencyInjection\ContainerInterface;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. class ShowPremiumProducts implements EventSubscriberInterface
  30. {
  31.     private CartService $cartService;
  32.     private PremiumDefinition $premium;
  33.     private ContainerInterface $container;
  34.     private FindPremiumProductsInCartInterface $findPremiumProducts;
  35.     private CheckIfVariantsAreAvailableInterface $checkIfVariantsAreAvailable;
  36.     private LoggerInterface $logger;
  37.     private SystemConfigService $systemConfigService;
  38.     private MinPriceRepositoryInterface $minPrice;
  39.     private RemoveProductsWithMaxMultiAddCounts $removeProductsWithMaxMultiAddCounts;
  40.     private TotalPriceRepositoryInterface $totalPrice;
  41.     private PremiumAccessControl $premiumAccessControl;
  42.     public function __construct(
  43.         CartService $cartService,
  44.         PremiumDefinition $premium,
  45.         ContainerInterface $container,
  46.         FindPremiumProductsInCartInterface $findPremiumProducts,
  47.         CheckIfVariantsAreAvailableInterface $checkIfVariantsAreAvailable,
  48.         LoggerInterface $logger,
  49.         SystemConfigService $systemConfigService,
  50.         MinPriceRepositoryInterface $minPrice,
  51.         RemoveProductsWithMaxMultiAddCounts $removeProductsWithMaxMultiAddCounts,
  52.         TotalPriceRepositoryInterface $totalPrice,
  53.         PremiumAccessControl $premiumAccessControl
  54.     ) {
  55.         $this->cartService $cartService;
  56.         $this->premium $premium;
  57.         $this->container $container;
  58.         $this->findPremiumProducts $findPremiumProducts;
  59.         $this->checkIfVariantsAreAvailable $checkIfVariantsAreAvailable;
  60.         $this->logger $logger;
  61.         $this->systemConfigService $systemConfigService;
  62.         $this->minPrice $minPrice;
  63.         $this->removeProductsWithMaxMultiAddCounts $removeProductsWithMaxMultiAddCounts;
  64.         $this->totalPrice $totalPrice;
  65.         $this->premiumAccessControl $premiumAccessControl;
  66.     }
  67.     public static function getSubscribedEvents(): array
  68.     {
  69.         return [
  70.             CheckoutCartPageLoadedEvent::class => 'onProductsLoaded',
  71.             CheckoutConfirmPageLoadedEvent::class => 'onProductsLoaded',
  72.         ];
  73.     }
  74.     public function onProductsLoaded($event): void
  75.     {
  76.         /** @var SalesChannelContext $salesChannelContext */
  77.         $salesChannelContext $event->getSalesChannelContext();
  78.         $salesChannel $event->getSalesChannelContext()->getSalesChannel();
  79.         $token $salesChannelContext->getToken();
  80.         $cart $this->cartService->getCart($token$salesChannelContext);
  81.         if ($this->premiumAccessControl->isAllowed($cart$salesChannelContext)) {
  82.             $ignoreStoreCount $this->systemConfigService->get(
  83.                 'BartCPreemiumArticle.config.ignoreStoreCount',
  84.                 $salesChannel->getId()
  85.             );
  86.             $multiAdd $this->systemConfigService->get(
  87.                 'BartCPreemiumArticle.config.multiadd',
  88.                 $salesChannel->getId()
  89.             );
  90.             $multiAddMax = (int) $this->systemConfigService->get(
  91.                 'BartCPreemiumArticle.config.multiaddMax',
  92.                 $salesChannel->getId()
  93.             );
  94.             $multiAddMaxInCart = (int) $this->systemConfigService->get(
  95.                 'BartCPreemiumArticle.config.multiaddMaxInCart',
  96.                 $salesChannel->getId()
  97.             );
  98.             $showOnConfirmPage $this->systemConfigService->get(
  99.                 'BartCPreemiumArticle.config.showpremiumonconfirm',
  100.                 $salesChannel->getId()
  101.             );
  102.             $showPremiumName $this->systemConfigService->get(
  103.                 'BartCPreemiumArticle.config.showpremiumname',
  104.                 $salesChannel->getId()
  105.             );
  106.             $premiumProductsInCart $this->findPremiumProducts->find($cart);
  107.             if ($premiumProductsInCart->count() === || $multiAdd) {
  108.                 /** @var EntityRepository $premiumCollection */
  109.                 $premiumCollection $this->container->get($this->premium->getEntityName() . '.repository');
  110.                 try {
  111.                     $criteria = new Criteria();
  112.                     $criteria->addFilter(
  113.                         new MultiFilter(
  114.                             NotFilter::CONNECTION_OR,
  115.                             [
  116.                                 new EqualsFilter(
  117.                                     'salesChannelId',
  118.                                     $salesChannelContext->getSalesChannel()->getId()
  119.                                 ),
  120.                                 new EqualsFilter(
  121.                                     'salesChannelId',
  122.                                     null
  123.                                 ),
  124.                             ]
  125.                         )
  126.                     );
  127.                     $criteria->addFilter(
  128.                         new MultiFilter(
  129.                             NotFilter::CONNECTION_OR,
  130.                             [
  131.                                 new EqualsFilter(
  132.                                     'customerGroupId',
  133.                                     $salesChannelContext->getCurrentCustomerGroup()->getId()
  134.                                 ),
  135.                                 new EqualsFilter(
  136.                                     'customerGroupId',
  137.                                     null
  138.                                 ),
  139.                             ]
  140.                         )
  141.                     );
  142.                     $criteria->addFilter(
  143.                         new EqualsFilter(
  144.                             'product.active',
  145.                             1
  146.                         )
  147.                     );
  148.                     if (!$ignoreStoreCount) {
  149.                         $criteria->addFilter(
  150.                                     new RangeFilter('product.availableStock', [
  151.                                         RangeFilter::GT => 0,
  152.                             ])
  153.                         );
  154.                     }
  155.                     $criteria->addSorting(
  156.                         new FieldSorting($this->premium->getEntityName() . '.minPrice')
  157.                     );
  158.                     $criteria->addAssociation('product.children');
  159.                     $criteria->addAssociation('product.children.options');
  160.                     $criteria->addAssociation('product.media');
  161.                 } catch (InconsistentCriteriaIdsException $e) {
  162.                     $this->logger->error(
  163.                         '[Premium Article] ' $e->getMessage(),
  164.                         [
  165.                             'file' => $e->getFile(),
  166.                             'line' => $e->getLine(),
  167.                         ]
  168.                     );
  169.                     return;
  170.                 }
  171.                 $premiumArticles $premiumCollection
  172.                     ->search(
  173.                         $criteria,
  174.                         $event->getContext()
  175.                     )->getElements();
  176.                 $premiumArticles $this->checkIfVariantsAreAvailable->remove($premiumArticles);
  177.                 if ($multiAddMaxInCart 0
  178.                     && $multiAddMaxInCart <= $premiumProductsInCart->getTotalQuantity()) {
  179.                     $premiumArticles = [];
  180.                 }
  181.                 if ($multiAddMax 0) {
  182.                     $premiumArticles $this->removeProductsWithMaxMultiAddCounts->remove($cart$premiumArticles$multiAddMax);
  183.                 }
  184.                 $totalPrice $this->totalPrice->get($cart$salesChannelContext);
  185.                 $products $this->findPremiumProducts->find($cart);
  186.                 $minPrice $this->minPrice->get($products);
  187.                 $event->getPage()->addExtension(
  188.                     'view',
  189.                     new ArrayEntity(
  190.                         [
  191.                             'premium_products' => $premiumArticles,
  192.                             'total_price' => $totalPrice,
  193.                             'currency_symbol' => $salesChannelContext->getCurrency()->getSymbol(),
  194.                             'multiAdd' => $multiAdd,
  195.                             'multiAddMax' => $multiAddMax,
  196.                             '$multiAddMaxInCart' => $multiAddMaxInCart,
  197.                             'minPrice' => $minPrice,
  198.                             'showPremiumOnConfirmPage' => $showOnConfirmPage,
  199.                             'showpremiumname' => $showPremiumName,
  200.                             'isConfirmPage' => (int) ($event->getPage() instanceof CheckoutConfirmPage),
  201.                         ]
  202.                     )
  203.                 );
  204.             }
  205.         }
  206.     }
  207. }