custom/plugins/SensusUptainConnect6/src/Storefront/Subscriber/UptainSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SensusUptainConnect6\Storefront\Subscriber;
  3. use SensusUptainConnect6\Storefront\Struct\CheckoutFinishStruct;
  4. use SensusUptainConnect6\Storefront\Struct\UptainConfig;
  5. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  6. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  7. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTax;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Event\StorefrontRenderEvent;
  11. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class UptainSubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var SystemConfigService */
  16.     protected $systemConfigService;
  17.     /** @var CartService */
  18.     protected $cartService;
  19.     public function __construct(SystemConfigService $systemConfigServiceCartService $cartService)
  20.     {
  21.         $this->systemConfigService $systemConfigService;
  22.         $this->cartService $cartService;
  23.     }
  24.     public function onStorefrontRender(StorefrontRenderEvent $event)
  25.     {
  26.         $context $event->getSalesChannelContext();
  27.         $currency $context->getCurrency();
  28.         $customer $context->getCustomer();
  29.         $customerGroup $context->getCurrentCustomerGroup();
  30.         $token $context->getToken();
  31.         $cart $this->cartService->getCart($token$context);
  32.         $promotionCode NULL;
  33.         foreach ($cart->getLineItems() as $item) {
  34.             if ($item instanceof LineItem && $item->getType() === LineItem::PROMOTION_LINE_ITEM_TYPE) {
  35.                 $promotionCode $item->getReferencedId();
  36.             }
  37.         }
  38.         if (!$customer || ($customer->getOrderCount() < && !$customer->getNewsletter())) {
  39.             $customer null;
  40.         }
  41.         $shippingCostTaxes 0;
  42.         /** @var CalculatedTax $calculatedTax */
  43.         foreach($cart->getShippingCosts()->getCalculatedTaxes() as $calculatedTax) {
  44.             $shippingCostTaxes += $calculatedTax->getTax();
  45.         }
  46.         $struct = new UptainConfig;
  47.         $struct->assign([
  48.             'active' => $this->systemConfigService->get('SensusUptainConnect6.config.active'$context $context->getSalesChannel()->getId() : null),
  49.             'uptainId' => $this->systemConfigService->get('SensusUptainConnect6.config.uptainId'$context $context->getSalesChannel()->getId() : null),
  50.             'token' => $token,
  51.             'price' => $cart->getPrice()->getNetPrice() - $cart->getShippingCosts()->getTotalPrice() + $shippingCostTaxes,
  52.             'currency' => $currency->getIsoCode(),
  53.             'customerEmail' => $customer $customer->getEmail() : null,
  54.             'customerFirstName' => $customer $customer->getFirstName() : null,
  55.             'customerLastName' => $customer $customer->getLastName() : null,
  56.             'customerTitle' => $customer $customer->getTitle() : null,
  57.             'customerGroup' => $customerGroup->getName(),
  58.             'voucherCode' => $promotionCode,
  59.         ]);
  60.         $event->setParameter('sensusUptainConnect'$struct);
  61.     }
  62.     public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event)
  63.     {
  64.         $order $event->getPage()->getOrder();
  65.         $promotionCode NULL;
  66.         foreach ($order->getLineItems() as $item) {
  67.             if ($item instanceof OrderLineItemEntity && $item->getType() === LineItem::PROMOTION_LINE_ITEM_TYPE) {
  68.                 $promotionCode $item->getReferencedId();
  69.             }
  70.         }
  71.         $struct = new CheckoutFinishStruct;
  72.         $struct->assign([
  73.             'orderNumber' => $order->getOrderNumber(),
  74.             'voucherCode' => $promotionCode,
  75.         ]);
  76.         $event->getPage()->addExtension('sensusUptainConnectFinish'$struct);
  77.     }
  78.     /**
  79.      * @return array<string>
  80.      */
  81.     public static function getSubscribedEvents(): array
  82.     {
  83.         return [
  84.             StorefrontRenderEvent::class => 'onStorefrontRender',
  85.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
  86.         ];
  87.     }
  88. }