custom/plugins/ChannelpilotTrackingSW6/src/Storefront/Subscriber/CheckoutFinishPageSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Chann\Channelpilot\Storefront\Subscriber;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Twig\Environment;
  10. use Twig\Extension\StringLoaderExtension;
  11. /**
  12.  * Adds our extensions to the checkout finished page
  13.  */
  14. class CheckoutFinishPageSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var SystemConfigService
  18.      */
  19.     private $systemConfigService;
  20.     /**
  21.      * @var Environment
  22.      */
  23.     private $twig;
  24.     /**
  25.      * @param SystemConfigService $systemConfigService
  26.      */
  27.     public function __construct(SystemConfigService $systemConfigServiceEnvironment $twig)
  28.     {
  29.         $this->systemConfigService $systemConfigService;
  30.         $this->twig $twig;
  31.     }
  32.     /**
  33.      * @inheritDoc
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             CheckoutFinishPageLoadedEvent::class => 'onPageLoaded'
  39.         ];
  40.     }
  41.     public function getPluginConfig()
  42.     {
  43.         return $this->systemConfigService->get('ChannelpilotTrackingSW6.config');
  44.     }
  45.     public function onPageLoaded(CheckoutFinishPageLoadedEvent $event): void
  46.     {
  47.         $this->registerTwigExtensions();
  48.         // get plugin config
  49.         $pluginConfig $this->getPluginConfig();
  50.         $orderEntity $event->getPage()->getOrder();
  51.         $orderLineItems $orderEntity->getLineItems()->filterByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  52.         $orderValueGross 0;
  53.         $shoppingCart = [];
  54.         foreach ($orderLineItems as $orderLineItem) {
  55.             /** @var OrderLineItemEntity $orderLineItem */
  56.             $sumOfTaxes 0;
  57.             foreach ($orderLineItem->getPrice()->getCalculatedTaxes() as $calculatedTax) {
  58.                 $sumOfTaxes += $calculatedTax->getTax();
  59.             }
  60.             $lineItemGross $orderLineItem->getPrice()->getTotalPrice() - $sumOfTaxes;
  61.             $orderValueGross += $lineItemGross;
  62.             $taxRate ?? 0;
  63.             if ($orderLineItem->getPrice()->getCalculatedTaxes()->first()) {
  64.                 $taxRate $orderLineItem->getPrice()->getCalculatedTaxes()->first()->getTaxRate() ?? 0;
  65.             }
  66.             $product = new \stdClass();
  67.             $product->id $orderLineItem->getPayload()['productNumber'];
  68.             $product->productname $orderLineItem->getLabel();
  69.             $product->price round($orderLineItem->getPrice()->getUnitPrice() / ((100 $taxRate) / 100), 2);
  70.             $product->amount $orderLineItem->getQuantity();
  71.             if (!empty($pluginConfig['customMarginField']) && !empty($orderLineItem->getPayload()['customFields'][$pluginConfig['customMarginField']])) {
  72.                 $margin $orderLineItem->getPayload()['customFields'][$pluginConfig['customMarginField']];
  73.                 $margin str_replace(',''.'$margin);
  74.                 $result preg_match('/^[-+]?[0-9]*\\.?[0-9]+$/'$margin);
  75.                 if ($result 0) {
  76.                     $product->margin round($margin2);
  77.                 }
  78.             }
  79.             $shoppingCart[] = $product;
  80.         }
  81.         $event->getPage()->addExtension('channelpilot_order', new \Shopware\Core\Framework\Struct\ArrayStruct(['orderTotal' => round($orderValueGross2)]));
  82.         $event->getPage()->addExtension('channelpilot_shoppingcart', new \Shopware\Core\Framework\Struct\ArrayStruct($shoppingCart));
  83.     }
  84.     private function registerTwigExtensions(): void
  85.     {
  86.         if (!$this->twig->hasExtension(StringLoaderExtension::class)) {
  87.             $this->twig->addExtension(new StringLoaderExtension);
  88.         }
  89.     }
  90. }