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

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.         if ((
  105.                 !$transmitCustomers
  106.                 || !$transmitNewsletterCustomers
  107.             )
  108.             && (
  109.                 !$customer
  110.                 || (
  111.                     $customer->getOrderCount() < 1
  112.                     && !$customer->getNewsletterSalesChannelIds()
  113.                 )
  114.             )
  115.         ) {
  116.             $customer null;
  117.         }
  118.         $transmitRevenue $this->systemConfigService->get(
  119.             'SensusUptainConnect6.config.transmitRevenue',
  120.             $context->getSalesChannel()->getId(),
  121.         );
  122.         $revenue null;
  123.         if ($customer && $transmitRevenue) {
  124.             /** @phpstan-ignore-next-line */
  125.             $revenue $customer->getOrderTotalAmount();
  126.         }
  127.         $cookieEnabled 1;
  128.         if ($blockCookies) {
  129.             $cookieEnabled 0;
  130.         }
  131.         $criteria = new Criteria;
  132.         $criteria->addFilter(new EqualsFilter('name''SensusUptainConnect6'));
  133.         $plugin $this->pluginRepository->search($criteria$event->getContext())->first();
  134.         $struct = new UptainConfig;
  135.         $struct->assign([
  136.             'active' => $this->systemConfigService->get(
  137.                 'SensusUptainConnect6.config.active',
  138.                 $context->getSalesChannel()->getId(),
  139.             ),
  140.             'uptainId' => $this->systemConfigService->get(
  141.                 'SensusUptainConnect6.config.uptainId',
  142.                 $context->getSalesChannel()->getId(),
  143.             ),
  144.             'token' => $token,
  145.             'customerEmail' => $customer $customer->getEmail() : null,
  146.             'customerFirstName' => $customer $customer->getFirstName() : null,
  147.             'customerLastName' => $customer $customer->getLastName() : null,
  148.             'customerTitle' => $customer $customer->getTitle() : null,
  149.             'customerUid' => $customer \md5('5f6384bfec4ca' $customer->getId()) : null,
  150.             'revenue' => $revenue,
  151.             'customerGroup' => $customerGroup->getName(),
  152.             'pluginData' => 'sensus-sw6:' $plugin->getVersion(),
  153.             'pageType' => $pageType,
  154.             'cookieEnabled' => $cookieEnabled,
  155.         ]);
  156.         $event->setParameter('sensusUptainConnect'$struct);
  157.     }
  158.     public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event)
  159.     {
  160.         $order $event->getPage()->getOrder();
  161.         $promotionCode null;
  162.         $cartProductsNetSum 0;
  163.         foreach ($order->getLineItems() as $item) {
  164.             if ($item->getPrice()) {
  165.                 $cartProductsNetSum += $item->getPrice()->getTotalPrice();
  166.                 $cartProductsNetSum -= $item->getPrice()->getCalculatedTaxes()->getAmount();
  167.             }
  168.             if (!($item instanceof OrderLineItemEntity) ||
  169.                 !\in_array($item->getType(), [
  170.                     'easy-coupon',
  171.                     LineItem::PROMOTION_LINE_ITEM_TYPE,
  172.                 ], true)) {
  173.                 continue;
  174.             }
  175.             $promotionCode $item->getReferencedId();
  176.         }
  177.         $struct = new CheckoutFinishStruct;
  178.         $struct->assign([
  179.             'orderNumber' => $order->getOrderNumber(),
  180.             'voucherCode' => $promotionCode,
  181.             'scv' => $cartProductsNetSum,
  182.         ]);
  183.         $event->getPage()->addExtension('sensusUptainConnectFinish'$struct);
  184.     }
  185.     public function onCustomerWishlistProductListingResultEvent(CustomerWishlistProductListingResultEvent $event)
  186.     {
  187.         $products $event->getResult();
  188.         if ($products->getTotal() <= 0) {
  189.             return;
  190.         }
  191.         $struct = new WhistlistStruct;
  192.         $context $event->getSalesChannelContext();
  193.         $whistlistArray = [];
  194.         foreach ($products->getEntities() as $product) {
  195.             $variants $product->getVariation();
  196.             $filterVariants \array_splice($variants0300);
  197.             $whistlistArray[] = [
  198.                 'amount' => 1,
  199.                 'price' => $product->getPrice()->getCurrencyPrice($context->getCurrencyId())->getGross(),
  200.                 'name' => $product->getTranslated()['name'],
  201.                 'variants' => $filterVariants,
  202.             ];
  203.         }
  204.         $struct->assign([
  205.             'wishlist' => \json_encode($whistlistArray),
  206.         ]);
  207.         $context->addExtension('sensusUptainConnectWhistlist'$struct);
  208.     }
  209.     public function onCartSavedEvent(CartSavedEvent $event)
  210.     {
  211.         $salesChannelContext $event->getSalesChannelContext();
  212.         $cartStruct $this->cartStructService->getCartStruct($salesChannelContext$event->getCart());
  213.         $salesChannelContext->addExtension('sensusUptainConnectCart'$cartStruct);
  214.     }
  215.     public function onProductListingResultEvent(ProductListingResultEvent $event)
  216.     {
  217.         $request $event->getRequest();
  218.         $context $event->getSalesChannelContext();
  219.         $parents = [];
  220.         $navigationId $request->get('navigationId'$context->getSalesChannel()->getNavigationCategoryId());
  221.         $category $this->categoryRepository->search(new Criteria([$navigationId]), $event->getContext())->first();
  222.         while ($parent $category->getParent()) {
  223.             if ($parent->getId() === $category->getId()) {
  224.                 break;
  225.             }
  226.             $parents[] = $parent->getName();
  227.         }
  228.         $productListArray = [];
  229.         $position 0;
  230.         foreach ($event->getResult()->getEntities() as $product) {
  231.             $productListArray[] = [
  232.                 'name' => $product->getTranslated()['name'],
  233.                 'position' => $position,
  234.             ];
  235.             $position += 1;
  236.         }
  237.         $struct = new CategoryStruct;
  238.         $struct->assign([
  239.             'categoryName' => $category->getName(),
  240.             'categoryPath' => \implode(';'\array_reverse($parents)),
  241.             'categoryProducts' => \json_encode($productListArray),
  242.             'categorySorting' => $event->getResult()->getSorting(),
  243.         ]);
  244.         $context->addExtension('sensusUptainProductListing'$struct);
  245.     }
  246.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event)
  247.     {
  248.         try {
  249.             $page $event->getPage();
  250.             $product $page->getProduct();
  251.             $breadcrumb = [];
  252.             $categoryName '';
  253.             if ($product->getSeoCategory()) {
  254.                 $breadcrumb $product->getSeoCategory()->getBreadcrumb();
  255.                 $categoryName $product->getSeoCategory()->getName();
  256.             }
  257.             $salesChannelContext $event->getSalesChannelContext();
  258.             $transmitProductDetails $this->systemConfigService->get(
  259.                 'SensusUptainConnect6.config.transmitProductDetails',
  260.                 $salesChannelContext->getSalesChannel()->getId(),
  261.             );
  262.             $struct = new ProductDetailStruct;
  263.             $struct->assign([
  264.                 'productCategory' => $categoryName,
  265.                 'productCategoryPaths' => \implode('/'$breadcrumb) . ';',
  266.             ]);
  267.             if ($transmitProductDetails) {
  268.                 $mediaUrl '';
  269.                 if ($product->getMedia()->count() > 0) {
  270.                     $mediaUrl $product->getMedia()->first()->getMedia()->getUrl();
  271.                 }
  272.                 $struct->assign([
  273.                     'productId' => \md5('5f6384bfec4ca' $product->getId()),
  274.                     'productName' => $product->getTranslated()['name'],
  275.                     'productPrice' => $product->getCalculatedCheapestPrice()->getTotalPrice(),
  276.                     'productOriginalPrice' => $product->getCalculatedPrice()->getTotalPrice(),
  277.                     'productImage' => $mediaUrl,
  278.                     'productTags' => $product->getTags(),
  279.                     'productVariants' => $product->getVariation(),
  280.                 ]);
  281.             }
  282.             $event->getPage()->addExtension('sensusUptainProductDetail'$struct);
  283.         } catch (\Throwable $ex) {
  284.             //ignore
  285.         }
  286.     }
  287.     public function onSearchPageLoadedEvent(SearchPageLoadedEvent $event)
  288.     {
  289.         $page $event->getPage();
  290.         $productListing $page->getListing();
  291.         $searchTerm $page->getSearchTerm();
  292.         $productListArray = [];
  293.         $position 0;
  294.         foreach ($productListing->getEntities() as $product) {
  295.             $productListArray[] = [
  296.                 'name' => $product->getTranslated()['name'],
  297.                 'position' => $position,
  298.             ];
  299.             $position += 1;
  300.         }
  301.         $struct = new SearchStruct;
  302.         $struct->assign([
  303.             'searchTerm' => $searchTerm,
  304.             'searchProducts' => \json_encode($productListArray),
  305.             'searchSorting' => $productListing->getSorting(),
  306.         ]);
  307.         $event->getPage()->addExtension('sensusUptainProductSearch'$struct);
  308.     }
  309.     /**
  310.      * @return array<string>
  311.      */
  312.     public static function getSubscribedEvents(): array
  313.     {
  314.         return [
  315.             StorefrontRenderEvent::class => 'onStorefrontRender',
  316.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded',
  317.             CustomerWishlistProductListingResultEvent::class => 'onCustomerWishlistProductListingResultEvent',
  318.             CartSavedEvent::class => 'onCartSavedEvent',
  319.             ProductListingResultEvent::class => 'onProductListingResultEvent',
  320.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
  321.             SearchPageLoadedEvent::class => 'onSearchPageLoadedEvent',
  322.         ];
  323.     }
  324. }