custom/plugins/mediameetsFbPixel/src/Storefront/Subscriber/AddDataToPage.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace mediameets\mediameetsFbPixel\Storefront\Subscriber;
  3. use mediameets\mediameetsFbPixel\Components\ContextualConfigReader;
  4. use mediameets\mediameetsFbPixel\Components\Data\PrepareCustomerData;
  5. use mediameets\mediameetsFbPixel\Components\Data\PrepareOrder;
  6. use mediameets\mediameetsFbPixel\Components\Struct\DataStruct;
  7. use Shopware\Core\Checkout\Customer\CustomerEntity;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class AddDataToPage implements EventSubscriberInterface
  16. {
  17.     private ContextualConfigReader $contextualConfigReader;
  18.     private SystemConfigService $configReader;
  19.     private EntityRepository $customerRepository;
  20.     public function __construct(
  21.         ContextualConfigReader $contextualConfigReader,
  22.         SystemConfigService $configReader,
  23.         EntityRepository $customerRepository
  24.     ) {
  25.         $this->customerRepository $customerRepository;
  26.         $this->configReader $configReader;
  27.         $this->contextualConfigReader $contextualConfigReader;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CheckoutFinishPageLoadedEvent::class => 'addDataForGuestsToCheckoutFinishPage',
  33.         ];
  34.     }
  35.     public function addDataForGuestsToCheckoutFinishPage(CheckoutFinishPageLoadedEvent $event): void
  36.     {
  37.         $config $this->contextualConfigReader->get();
  38.         if ($config === null) {
  39.             return;
  40.         }
  41.         if (! \array_key_exists('pixelIds'$config) || $config['pixelIds'] === '') {
  42.             return;
  43.         }
  44.         $salesChannelId $event->getSalesChannelContext()->getSalesChannelId();
  45.         if ($this->configReader->getBool('core.cart.logoutGuestAfterCheckout'$salesChannelId) === false) {
  46.             return;
  47.         }
  48.         $order $event->getPage()->getOrder();
  49.         $orderCustomer $order->getOrderCustomer();
  50.         if (! $orderCustomer instanceof OrderCustomerEntity) {
  51.             return;
  52.         }
  53.         $customer $orderCustomer->getCustomer();
  54.         if (! $customer instanceof CustomerEntity || $customer->getGuest() === false) {
  55.             return;
  56.         }
  57.         $customerData null;
  58.         if (\array_key_exists('advancedMatching'$config) && $config['advancedMatching'] === true) {
  59.             $criteria = new Criteria();
  60.             $criteria->addAssociation('salutation');
  61.             $criteria->addAssociation('defaultShippingAddress.country');
  62.             $criteria->addAssociation('defaultShippingAddress.countryState');
  63.             $criteria->addAssociation('defaultShippingAddress.salutation');
  64.             $criteria->addFilter(new EqualsFilter('id'$customer->getId()));
  65.             $customer $this->customerRepository->search($criteria$event->getContext())->first();
  66.             if ($customer instanceof CustomerEntity) {
  67.                 $customerData PrepareCustomerData::handle($customer);
  68.             }
  69.         }
  70.         $dataStruct = new DataStruct();
  71.         $dataStruct->assign([
  72.             'customer' => $customerData,
  73.             'order' => PrepareOrder::handle($order),
  74.         ]);
  75.         $event->getPage()->addExtension('mediameetsFbPixel'$dataStruct);
  76.     }
  77. }