vendor/crehler/mojebambino-engine/src/Subscriber/CheckoutCartSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   03 lip 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\MojeBambinoEngine\Subscriber;
  13. use Crehler\MojeBambinoEngine\Checkout\Cart\CheckoutCrossSelling\CheckoutCrossSellingBuilder;
  14. use Crehler\MojeBambinoEngine\Service\ProductStatusStorefrontService;
  15. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Twig\Environment;
  19. class CheckoutCartSubscriber implements EventSubscriberInterface
  20. {
  21.     private CheckoutCrossSellingBuilder $checkoutCrossSellingBuilder;
  22.     private ProductStatusStorefrontService $productStatus;
  23.     private Environment $environment;
  24.     public function __construct(
  25.         CheckoutCrossSellingBuilder    $checkoutCrossSellingBuilder,
  26.         ProductStatusStorefrontService $productStatus,
  27.         Environment                    $environment
  28.     )
  29.     {
  30.         $this->checkoutCrossSellingBuilder $checkoutCrossSellingBuilder;
  31.         $this->productStatus $productStatus;
  32.         $this->environment $environment;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CheckoutCartPageLoadedEvent::class => 'onCheckoutPageLoaded',
  38.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPage',
  39.         ];
  40.     }
  41.     public function onCheckoutPageLoaded(CheckoutCartPageLoadedEvent $event): void
  42.     {
  43.         $this->environment->addGlobal('isCheckoutCartPage'true);
  44.         $context $event->getContext();
  45.         $page $event->getPage();
  46.         $cart $page->getCart();
  47.         $productId = [];
  48.         foreach ($cart->getLineItems() as $key => $lineItem) {
  49.             if ($lineItem->getType() !== 'product') {
  50.                 continue;
  51.             }
  52.             $productId[] = $lineItem->getReferencedId();
  53.         }
  54.         $page->addExtension(
  55.             'checkoutCrossSellingStream',
  56.             $this->checkoutCrossSellingBuilder->buildCheckoutProductStream($productId$event->getSalesChannelContext()));
  57.         foreach ($cart->getLineItems() as $lineItem) {
  58.             if ($lineItem->getType() === 'product') {
  59.                 $lineItem->addExtension(
  60.                     'productStatus',
  61.                     $this->productStatus->findProductStatus($lineItem->getReferencedId(), $context)
  62.                 );
  63.             }
  64.         }
  65.     }
  66.     public function onCheckoutConfirmPage(CheckoutConfirmPageLoadedEvent $event): void
  67.     {
  68.         $context $event->getContext();
  69.         $page $event->getPage();
  70.         $cart $page->getCart();
  71.         foreach ($cart->getLineItems() as $lineItem) {
  72.             if ($lineItem->getType() === 'product') {
  73.                 $lineItem->addExtension(
  74.                     'productStatus',
  75.                     $this->productStatus->findProductStatus($lineItem->getReferencedId(), $context)
  76.                 );
  77.             }
  78.         }
  79.     }
  80. }