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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SensusUptainConnect6\Storefront\Subscriber;
  3. use SensusUptainConnect6\Storefront\Service\CartStructService;
  4. use SensusUptainConnect6\Storefront\Struct\CategoryStruct;
  5. use SensusUptainConnect6\Storefront\Struct\CheckoutFinishStruct;
  6. use SensusUptainConnect6\Storefront\Struct\ProductDetailStruct;
  7. use SensusUptainConnect6\Storefront\Struct\SearchStruct;
  8. use SensusUptainConnect6\Storefront\Struct\UptainConfig;
  9. use SensusUptainConnect6\Storefront\Struct\WhistlistStruct;
  10. use Shopware\Core\Checkout\Cart\Event\CartSavedEvent;
  11. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  12. use Shopware\Core\Checkout\Customer\Event\CustomerWishlistProductListingResultEvent;
  13. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  14. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  18. use Shopware\Core\System\SystemConfig\SystemConfigService;
  19. use Shopware\Storefront\Event\StorefrontRenderEvent;
  20. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  21. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  22. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  23. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. class UptainSubscriber implements EventSubscriberInterface
  26. {
  27.     protected ?SystemConfigService $systemConfigService;
  28.     protected ?EntityRepositoryInterface $pluginRepository;
  29.     protected ?GenericPageLoaderInterface $genericPageLoader;
  30.     protected ?EntityRepositoryInterface $categoryRepository;
  31.     protected ?EntityRepositoryInterface $cmsPageRepository;
  32.     protected ?EntityRepositoryInterface $wishlistRepository;
  33.     protected ?EntityRepositoryInterface $productRepository;
  34.     protected ?CartStructService $cartStructService;
  35.     // phpcs:disable
  36.     public function __construct(
  37.         SystemConfigService $systemConfigService,
  38.         EntityRepositoryInterface $pluginRepository,
  39.         GenericPageLoaderInterface $genericPageLoader,
  40.         EntityRepositoryInterface $categoryRepository,
  41.         EntityRepositoryInterface $cmsPageRepository,
  42.         EntityRepositoryInterface $wishlistRepository,
  43.         EntityRepositoryInterface $productRepository,
  44.         CartStructService $cartStructService
  45.     ) {
  46.         $this->systemConfigService $systemConfigService;
  47.         $this->pluginRepository $pluginRepository;
  48.         $this->genericPageLoader $genericPageLoader;
  49.         $this->categoryRepository $categoryRepository;
  50.         $this->cmsPageRepository $cmsPageRepository;
  51.         $this->wishlistRepository $wishlistRepository;
  52.         $this->productRepository $productRepository;
  53.         $this->cartStructService $cartStructService;
  54.     }
  55.     // phpcs:enable
  56.     public function onStorefrontRender(StorefrontRenderEvent $event)
  57.     {
  58.         $context $event->getSalesChannelContext();
  59.         $customer $context->getCustomer();
  60.         $customerGroup $context->getCurrentCustomerGroup();
  61.         $token $context->getToken();
  62.         $route $event->getRequest()->get('_route');
  63.         // phpcs:disable
  64.         switch ($route) {
  65.             case 'frontend.navigation.page':
  66.                 $pageType 'category';
  67.                 break;
  68.             case 'frontend.detail.page':
  69.                 $pageType 'product';
  70.                 break;
  71.             case 'frontend.checkout.finish.page':
  72.                 $pageType 'success';
  73.                 break;
  74.             case 'frontend.search.page':
  75.                 $pageType 'search';
  76.                 break;
  77.             case 'frontend.checkout.register.page':
  78.             case 'frontend.checkout.confirm.page':
  79.                 $pageType 'checkout';
  80.                 break;
  81.             case 'frontend.checkout.cart.page':
  82.                 $pageType 'cart';
  83.                 break;
  84.             case 'frontend.home.page':
  85.                 $pageType 'home';
  86.                 break;
  87.             default:
  88.                 $pageType 'other';
  89.                 break;
  90.         }
  91.         // phpcs:enable
  92.         $transmitNewsletterCustomers $this->systemConfigService->get(
  93.             'SensusUptainConnect6.config.transmitNewsletterCustomers',
  94.             $context->getSalesChannel()->getId(),
  95.         );
  96.         $transmitCustomers $this->systemConfigService->get(
  97.             'SensusUptainConnect6.config.transmitCustomers',
  98.             $context->getSalesChannel()->getId(),
  99.         );
  100.         $blockCookies $this->systemConfigService->get(
  101.             'SensusUptainConnect6.config.blockCookies',
  102.             $context->getSalesChannel()->getId(),
  103.         );
  104.         $customer = ($transmitCustomers && $customer && $customer->getOrderCount() > 0)
  105.             || ($transmitNewsletterCustomers && $customer && $customer->getNewsletterSalesChannelIds() && count($customer->getNewsletterSalesChannelIds()) > 0)
  106.                 ? $customer
  107.                 null;
  108.         $transmitRevenue $this->systemConfigService->get(
  109.             'SensusUptainConnect6.config.transmitRevenue',
  110.             $context->getSalesChannel()->getId(),
  111.         );
  112.         $revenue null;
  113.         if ($customer && $transmitRevenue) {
  114.             /** @phpstan-ignore-next-line */
  115.             $revenue $customer->getOrderTotalAmount();
  116.         }
  117.         $cookieEnabled 1;
  118.         if ($blockCookies) {
  119.             $cookieEnabled 0;
  120.         }
  121.         $criteria = new Criteria;
  122.         $criteria->addFilter(new EqualsFilter('name''SensusUptainConnect6'));
  123.         $plugin $this->pluginRepository->search($criteria$event->getContext())->first();
  124.         $struct = new UptainConfig;
  125.         $struct->assign([
  126.             'active' => $this->systemConfigService->get(
  127.                 'SensusUptainConnect6.config.active',
  128.                 $context->getSalesChannel()->getId(),
  129.             ),
  130.             'uptainId' => $this->systemConfigService->get(
  131.                 'SensusUptainConnect6.config.uptainId',
  132.                 $context->getSalesChannel()->getId(),
  133.             ),
  134.             'token' => $token,
  135.             'customerEmail' => $customer $customer->getEmail() : null,
  136.             'customerFirstName' => $customer $customer->getFirstName() : null,
  137.             'customerLastName' => $customer $customer->getLastName() : null,
  138.             'customerTitle' => $customer $customer->getTitle() : null,
  139.             'customerUid' => $customer \md5('5f6384bfec4ca' $customer->getId()) : null,
  140.             'revenue' => $revenue,
  141.             'customerGroup' => $customerGroup->getName(),
  142.             'pluginData' => 'sensus-sw6:' $plugin->getVersion(),
  143.             'pageType' => $pageType,
  144.             'cookieEnabled' => $cookieEnabled,
  145.         ]);
  146.         $event->setParameter('sensusUptainConnect'$struct);
  147.     }
  148.     public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event)
  149.     {
  150.         $order $event->getPage()->getOrder();
  151.         $promotionCode null;
  152.         $cartProductsNetSum 0;
  153.         foreach ($order->getLineItems() as $item) {
  154.             if ($item->getPrice()) {
  155.                 $cartProductsNetSum += $item->getPrice()->getTotalPrice();
  156.                 $cartProductsNetSum -= $item->getPrice()->getCalculatedTaxes()->getAmount();
  157.             }
  158.             if (!($item instanceof OrderLineItemEntity) ||
  159.                 !\in_array($item->getType(), [
  160.                     'easy-coupon',
  161.                     LineItem::PROMOTION_LINE_ITEM_TYPE,
  162.                 ], true)) {
  163.                 continue;
  164.             }
  165.             $promotionCode $item->getReferencedId();
  166.         }
  167.         $struct = new CheckoutFinishStruct;
  168.         $struct->assign([
  169.             'orderNumber' => $order->getOrderNumber(),
  170.             'voucherCode' => $promotionCode,
  171.             'scv' => $cartProductsNetSum,
  172.         ]);
  173.         $event->getPage()->addExtension('sensusUptainConnectFinish'$struct);
  174.     }
  175.     public function onCustomerWishlistProductListingResultEvent(CustomerWishlistProductListingResultEvent $event)
  176.     {
  177.         $products $event->getResult();
  178.         if ($products->getTotal() <= 0) {
  179.             return;
  180.         }
  181.         $struct = new WhistlistStruct;
  182.         $context $event->getSalesChannelContext();
  183.         $whistlistArray = [];
  184.         foreach ($products->getEntities() as $product) {
  185.             $variants $product->getVariation();
  186.             $filterVariants \array_splice($variants0300);
  187.             $whistlistArray[] = [
  188.                 'amount' => 1,
  189.                 'price' => $product->getPrice()->getCurrencyPrice($context->getCurrencyId())->getGross(),
  190.                 'name' => $product->getTranslated()['name'],
  191.                 'variants' => $filterVariants,
  192.             ];
  193.         }
  194.         $struct->assign([
  195.             'wishlist' => \json_encode($whistlistArray),
  196.         ]);
  197.         $context->addExtension('sensusUptainConnectWhistlist'$struct);
  198.     }
  199.     public function onCartSavedEvent(CartSavedEvent $event)
  200.     {
  201.         $salesChannelContext $event->getSalesChannelContext();
  202.         $cartStruct $this->cartStructService->getCartStruct($salesChannelContext$event->getCart());
  203.         $salesChannelContext->addExtension('sensusUptainConnectCart'$cartStruct);
  204.     }
  205.     public function onProductListingResultEvent(ProductListingResultEvent $event)
  206.     {
  207.         $request $event->getRequest();
  208.         $context $event->getSalesChannelContext();
  209.         $parents = [];
  210.         $navigationId $request->get('navigationId'$context->getSalesChannel()->getNavigationCategoryId());
  211.         $category $this->categoryRepository->search(new Criteria([$navigationId]), $event->getContext())->first();
  212.         while ($parent $category->getParent()) {
  213.             if ($parent->getId() === $category->getId()) {
  214.                 break;
  215.             }
  216.             $parents[] = $parent->getName();
  217.         }
  218.         $productListArray = [];
  219.         $position 0;
  220.         foreach ($event->getResult()->getEntities() as $product) {
  221.             $productListArray[] = [
  222.                 'name' => $product->getTranslated()['name'],
  223.                 'position' => $position,
  224.             ];
  225.             $position += 1;
  226.         }
  227.         $struct = new CategoryStruct;
  228.         $struct->assign([
  229.             'categoryName' => $category->getName(),
  230.             'categoryPath' => \implode(';'\array_reverse($parents)),
  231.             'categoryProducts' => \json_encode($productListArray),
  232.             'categorySorting' => $event->getResult()->getSorting(),
  233.         ]);
  234.         $context->addExtension('sensusUptainProductListing'$struct);
  235.     }
  236.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event)
  237.     {
  238.         try {
  239.             $page $event->getPage();
  240.             $product $page->getProduct();
  241.             $breadcrumb = [];
  242.             $categoryName '';
  243.             if ($product->getSeoCategory()) {
  244.                 $breadcrumb $product->getSeoCategory()->getBreadcrumb();
  245.                 $categoryName $product->getSeoCategory()->getName();
  246.             }
  247.             $salesChannelContext $event->getSalesChannelContext();
  248.             $transmitProductDetails $this->systemConfigService->get(
  249.                 'SensusUptainConnect6.config.transmitProductDetails',
  250.                 $salesChannelContext->getSalesChannel()->getId(),
  251.             );
  252.             $struct = new ProductDetailStruct;
  253.             $struct->assign([
  254.                 'productCategory' => $categoryName,
  255.                 'productCategoryPaths' => \implode('/'$breadcrumb) . ';',
  256.             ]);
  257.             if ($transmitProductDetails) {
  258.                 $mediaUrl '';
  259.                 if ($product->getMedia()->count() > 0) {
  260.                     $mediaUrl $product->getMedia()->first()->getMedia()->getUrl();
  261.                 }
  262.                 $struct->assign([
  263.                     'productId' => \md5('5f6384bfec4ca' $product->getId()),
  264.                     'productName' => $product->getTranslated()['name'],
  265.                     'productPrice' => $product->getCalculatedCheapestPrice()->getTotalPrice(),
  266.                     'productOriginalPrice' => $product->getCalculatedPrice()->getTotalPrice(),
  267.                     'productImage' => $mediaUrl,
  268.                     'productTags' => $product->getTags(),
  269.                     'productVariants' => $product->getVariation(),
  270.                 ]);
  271.             }
  272.             $event->getPage()->addExtension('sensusUptainProductDetail'$struct);
  273.         } catch (\Throwable $ex) {
  274.             //ignore
  275.         }
  276.     }
  277.     public function onSearchPageLoadedEvent(SearchPageLoadedEvent $event)
  278.     {
  279.         $page $event->getPage();
  280.         $productListing $page->getListing();
  281.         $searchTerm $page->getSearchTerm();
  282.         $productListArray = [];
  283.         $position 0;
  284.         foreach ($productListing->getEntities() as $product) {
  285.             $productListArray[] = [
  286.                 'name' => $product->getTranslated()['name'],
  287.                 'position' => $position,
  288.             ];
  289.             $position += 1;
  290.         }
  291.         $struct = new SearchStruct;
  292.         $struct->assign([
  293.             'searchTerm' => $searchTerm,
  294.             'searchProducts' => \json_encode($productListArray),
  295.             'searchSorting' => $productListing->getSorting(),
  296.         ]);
  297.         $event->getPage()->addExtension('sensusUptainProductSearch'$struct);
  298.     }
  299.     /**
  300.      * @return array<string>
  301.      */
  302.     public static function getSubscribedEvents(): array
  303.     {
  304.         return [
  305.             StorefrontRenderEvent::class => 'onStorefrontRender',
  306.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
  307.             CustomerWishlistProductListingResultEvent::class => 'onCustomerWishlistProductListingResultEvent',
  308.             CartSavedEvent::class => 'onCartSavedEvent',
  309.             ProductListingResultEvent::class => 'onProductListingResultEvent',
  310.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
  311.             SearchPageLoadedEvent::class => 'onSearchPageLoadedEvent',
  312.         ];
  313.     }
  314. }