custom/plugins/BartCPreemiumArticle/src/Subscriber/ShowCanvasButton.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
  4. use BartCPreemiumArticle\PremiumArticle\Repository\MinPriceRepositoryInterface;
  5. use BartCPreemiumArticle\PremiumArticle\Repository\TotalPriceRepositoryInterface;
  6. use BartCPreemiumArticle\PremiumArticle\Service\CheckIfCustomerGroupHasAnyPremiumArticleInterface;
  7. use BartCPreemiumArticle\PremiumArticle\Service\FindPremiumProductsInCartInterface;
  8. use Shopware\Core\Framework\Struct\ArrayEntity;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ShowCanvasButton implements EventSubscriberInterface
  13. {
  14.     private SystemConfigService $systemConfigService;
  15.     private FindPremiumProductsInCartInterface $findPremiumProducts;
  16.     private MinPriceRepositoryInterface $minPrice;
  17.     private TotalPriceRepositoryInterface $totalPrice;
  18.     private CheckIfCustomerGroupHasAnyPremiumArticleInterface $checkIfCustomerGroupHasAnyPremiumArticle;
  19.     public function __construct(
  20.         SystemConfigService $systemConfigService,
  21.         FindPremiumProductsInCartInterface $findPremiumProducts,
  22.         MinPriceRepositoryInterface $minPrice,
  23.         TotalPriceRepositoryInterface $totalPrice,
  24.         CheckIfCustomerGroupHasAnyPremiumArticleInterface $checkIfCustomerGroupHasAnyPremiumArticle
  25.     ) {
  26.         $this->systemConfigService $systemConfigService;
  27.         $this->findPremiumProducts $findPremiumProducts;
  28.         $this->minPrice $minPrice;
  29.         $this->totalPrice $totalPrice;
  30.         $this->checkIfCustomerGroupHasAnyPremiumArticle $checkIfCustomerGroupHasAnyPremiumArticle;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             OffcanvasCartPageLoadedEvent::class => 'onCanvaLoad',
  36.         ];
  37.     }
  38.     public function onCanvaLoad(OffcanvasCartPageLoadedEvent $event): void
  39.     {
  40.         $cart $event->getPage()->getCart();
  41.         $multiAdd $this->systemConfigService->get(
  42.             'BartCPreemiumArticle.config.multiadd',
  43.             $event->getSalesChannelContext()->getSalesChannel()->getId()
  44.         );
  45.         $multiAddMax $this->systemConfigService->get(
  46.             'BartCPreemiumArticle.config.multiaddMax',
  47.             $event->getSalesChannelContext()->getSalesChannel()->getId()
  48.         );
  49.         $totalPrice $this->totalPrice->get($cart$event->getSalesChannelContext());
  50.         $products $this->findPremiumProducts->find($cart);
  51.         $minPrice $this->minPrice->get($products);
  52.         $isSomePremiumArticleInCurrentCustomerGroup
  53.             $this->checkIfCustomerGroupHasAnyPremiumArticle->get(
  54.                 $event->getSalesChannelContext()->getCustomer(),
  55.                 $event->getSalesChannelContext()
  56.             );
  57.         $event->getPage()->addExtension(
  58.             'view',
  59.             new ArrayEntity(
  60.                 [
  61.                     'show_button_in_canvas' => $this->systemConfigService->get(
  62.                         'BartCPreemiumArticle.config.showbuttonincanvascart',
  63.                         $event->getSalesChannelContext()->getSalesChannel()->getId()
  64.                     ),
  65.                     'isSomePremiumArticleInCurrentCustomerGroup' => $isSomePremiumArticleInCurrentCustomerGroup,
  66.                     'total_price' => $totalPrice,
  67.                     'multiAdd' => $multiAdd,
  68.                     'multiAddMax' => $multiAddMax,
  69.                     'minPrice' => $minPrice,
  70.                 ]
  71.             )
  72.         );
  73.     }
  74. }