custom/plugins/TanmarNgGoogleAdsTracking/src/Storefront/Page/Product/ProductPageSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tanmar\GoogleAdsTracking\Storefront\Page\Product;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  6. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  7. use Tanmar\GoogleAdsTracking\Storefront\BaseSubscriber;
  8. use Tanmar\GoogleAdsTracking\Components\Helper\LoggerHelper;
  9. use Tanmar\GoogleAdsTracking\Service\ConfigService;
  10. use Tanmar\GoogleAdsTracking\Service\Article\ArticleIdentifierService;
  11. use Tanmar\GoogleAdsTracking\Service\RemarketingEvent\RemarketingEventService;
  12. class ProductPageSubscriber extends BaseSubscriber implements EventSubscriberInterface {
  13.     protected RemarketingEventService $remarketingEventService;
  14.     
  15.     public function __construct(ConfigService $configServiceLoggerHelper $loggerHelperArticleIdentifierService $articleIdentifierServiceRemarketingEventService $remarketingEventService) {
  16.         parent::__construct($configService$loggerHelper$articleIdentifierService);
  17.         $this->remarketingEventService $remarketingEventService;
  18.     }
  19.     public static function getSubscribedEvents(): array {
  20.         return [
  21.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  22.         ];
  23.     }
  24.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void {
  25.         try {
  26.             $googleAdsData $this->getExtension($event);
  27.             if (!is_null($googleAdsData) && $this->getConfig()->isRetargetingActive()) {
  28.                 $eventData $this->remarketingEventService->getEventData(array($event->getPage()->getProduct()), $this->getPrice($event->getPage()->getProduct()));
  29.                 $googleAdsData->assign([
  30.                     'eventName' => RemarketingEventService::GOOGLE_ADS_EVENT_NAME_PRODUCT,
  31.                     'eventData' => (count($eventData) > 0) ? $eventData false
  32.                 ]);
  33.             }
  34.             $this->addExtension($event$googleAdsData);
  35.         } catch (\Throwable $e) {
  36.             $this->loggerHelper->logThrowable($e);
  37.         }
  38.     }
  39.     protected function getPrice(SalesChannelProductEntity $product): float {
  40.         $price 0;
  41.         try {
  42.             $calculatedPrices $product->getCalculatedPrices();
  43.             $calculatedPrice $product->getCalculatedPrice();
  44.             if (!is_null($calculatedPrices) && !is_null($calculatedPrices->getElements()) && count($calculatedPrices->getElements()) > 0) {
  45.                 $price $calculatedPrices->getElements()[0]->getUnitPrice();
  46.             } elseif (!is_null($calculatedPrice) && !is_null($calculatedPrice->getTotalPrice())) {
  47.                 $price $calculatedPrice->getTotalPrice();
  48.             }
  49.         } catch (\Throwable $e) {
  50.             $this->loggerHelper->logThrowable($e);
  51.         }
  52.         return $price;
  53.     }
  54. }