<?php
declare(strict_types=1);
namespace Chann\Channelpilot\Storefront\Subscriber;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;
use Twig\Extension\StringLoaderExtension;
/**
* Adds our extensions to the checkout finished page
*/
class CheckoutFinishPageSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var Environment
*/
private $twig;
/**
* @param SystemConfigService $systemConfigService
*/
public function __construct(SystemConfigService $systemConfigService, Environment $twig)
{
$this->systemConfigService = $systemConfigService;
$this->twig = $twig;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
CheckoutFinishPageLoadedEvent::class => 'onPageLoaded'
];
}
public function getPluginConfig()
{
return $this->systemConfigService->get('ChannelpilotTrackingSW6.config');
}
public function onPageLoaded(CheckoutFinishPageLoadedEvent $event): void
{
$this->registerTwigExtensions();
// get plugin config
$pluginConfig = $this->getPluginConfig();
$orderEntity = $event->getPage()->getOrder();
$orderLineItems = $orderEntity->getLineItems()->filterByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
$orderValueGross = 0;
$shoppingCart = [];
foreach ($orderLineItems as $orderLineItem) {
/** @var OrderLineItemEntity $orderLineItem */
$sumOfTaxes = 0;
foreach ($orderLineItem->getPrice()->getCalculatedTaxes() as $calculatedTax) {
$sumOfTaxes += $calculatedTax->getTax();
}
$lineItemGross = $orderLineItem->getPrice()->getTotalPrice() - $sumOfTaxes;
$orderValueGross += $lineItemGross;
$taxRate = 0 ?? 0;
if ($orderLineItem->getPrice()->getCalculatedTaxes()->first()) {
$taxRate = $orderLineItem->getPrice()->getCalculatedTaxes()->first()->getTaxRate() ?? 0;
}
$product = new \stdClass();
$product->id = $orderLineItem->getPayload()['productNumber'];
$product->productname = $orderLineItem->getLabel();
$product->price = round($orderLineItem->getPrice()->getUnitPrice() / ((100 + $taxRate) / 100), 2);
$product->amount = $orderLineItem->getQuantity();
if (!empty($pluginConfig['customMarginField']) && !empty($orderLineItem->getPayload()['customFields'][$pluginConfig['customMarginField']])) {
$margin = $orderLineItem->getPayload()['customFields'][$pluginConfig['customMarginField']];
$margin = str_replace(',', '.', $margin);
$result = preg_match('/^[-+]?[0-9]*\\.?[0-9]+$/', $margin);
if ($result > 0) {
$product->margin = round($margin, 2);
}
}
$shoppingCart[] = $product;
}
$event->getPage()->addExtension('channelpilot_order', new \Shopware\Core\Framework\Struct\ArrayStruct(['orderTotal' => round($orderValueGross, 2)]));
$event->getPage()->addExtension('channelpilot_shoppingcart', new \Shopware\Core\Framework\Struct\ArrayStruct($shoppingCart));
}
private function registerTwigExtensions(): void
{
if (!$this->twig->hasExtension(StringLoaderExtension::class)) {
$this->twig->addExtension(new StringLoaderExtension);
}
}
}