<?php declare(strict_types=1);
namespace SensusUptainConnect6\Storefront\Subscriber;
use SensusUptainConnect6\Storefront\Struct\CheckoutFinishStruct;
use SensusUptainConnect6\Storefront\Struct\UptainConfig;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTax;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UptainSubscriber implements EventSubscriberInterface
{
/** @var SystemConfigService */
protected $systemConfigService;
/** @var CartService */
protected $cartService;
public function __construct(SystemConfigService $systemConfigService, CartService $cartService)
{
$this->systemConfigService = $systemConfigService;
$this->cartService = $cartService;
}
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$context = $event->getSalesChannelContext();
$currency = $context->getCurrency();
$customer = $context->getCustomer();
$customerGroup = $context->getCurrentCustomerGroup();
$token = $context->getToken();
$cart = $this->cartService->getCart($token, $context);
$promotionCode = NULL;
foreach ($cart->getLineItems() as $item) {
if ($item instanceof LineItem && $item->getType() === LineItem::PROMOTION_LINE_ITEM_TYPE) {
$promotionCode = $item->getReferencedId();
}
}
if (!$customer || ($customer->getOrderCount() < 1 && !$customer->getNewsletter())) {
$customer = null;
}
$shippingCostTaxes = 0;
/** @var CalculatedTax $calculatedTax */
foreach($cart->getShippingCosts()->getCalculatedTaxes() as $calculatedTax) {
$shippingCostTaxes += $calculatedTax->getTax();
}
$struct = new UptainConfig;
$struct->assign([
'active' => $this->systemConfigService->get('SensusUptainConnect6.config.active', $context ? $context->getSalesChannel()->getId() : null),
'uptainId' => $this->systemConfigService->get('SensusUptainConnect6.config.uptainId', $context ? $context->getSalesChannel()->getId() : null),
'token' => $token,
'price' => $cart->getPrice()->getNetPrice() - $cart->getShippingCosts()->getTotalPrice() + $shippingCostTaxes,
'currency' => $currency->getIsoCode(),
'customerEmail' => $customer ? $customer->getEmail() : null,
'customerFirstName' => $customer ? $customer->getFirstName() : null,
'customerLastName' => $customer ? $customer->getLastName() : null,
'customerTitle' => $customer ? $customer->getTitle() : null,
'customerGroup' => $customerGroup->getName(),
'voucherCode' => $promotionCode,
]);
$event->setParameter('sensusUptainConnect', $struct);
}
public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event)
{
$order = $event->getPage()->getOrder();
$promotionCode = NULL;
foreach ($order->getLineItems() as $item) {
if ($item instanceof OrderLineItemEntity && $item->getType() === LineItem::PROMOTION_LINE_ITEM_TYPE) {
$promotionCode = $item->getReferencedId();
}
}
$struct = new CheckoutFinishStruct;
$struct->assign([
'orderNumber' => $order->getOrderNumber(),
'voucherCode' => $promotionCode,
]);
$event->getPage()->addExtension('sensusUptainConnectFinish', $struct);
}
/**
* @return array<string>
*/
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender',
CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
];
}
}