<?php declare(strict_types=1);
namespace mediameets\mediameetsFbPixel\Storefront\Subscriber;
use mediameets\mediameetsFbPixel\Components\ContextualConfigReader;
use mediameets\mediameetsFbPixel\Components\Data\PrepareCustomerData;
use mediameets\mediameetsFbPixel\Components\Data\PrepareOrder;
use mediameets\mediameetsFbPixel\Components\Struct\DataStruct;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AddDataToPage implements EventSubscriberInterface
{
private ContextualConfigReader $contextualConfigReader;
private SystemConfigService $configReader;
private EntityRepository $customerRepository;
public function __construct(
ContextualConfigReader $contextualConfigReader,
SystemConfigService $configReader,
EntityRepository $customerRepository
) {
$this->customerRepository = $customerRepository;
$this->configReader = $configReader;
$this->contextualConfigReader = $contextualConfigReader;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutFinishPageLoadedEvent::class => 'addDataForGuestsToCheckoutFinishPage',
];
}
public function addDataForGuestsToCheckoutFinishPage(CheckoutFinishPageLoadedEvent $event): void
{
$config = $this->contextualConfigReader->get();
if ($config === null) {
return;
}
if (! \array_key_exists('pixelIds', $config) || $config['pixelIds'] === '') {
return;
}
$salesChannelId = $event->getSalesChannelContext()->getSalesChannelId();
if ($this->configReader->getBool('core.cart.logoutGuestAfterCheckout', $salesChannelId) === false) {
return;
}
$order = $event->getPage()->getOrder();
$orderCustomer = $order->getOrderCustomer();
if (! $orderCustomer instanceof OrderCustomerEntity) {
return;
}
$customer = $orderCustomer->getCustomer();
if (! $customer instanceof CustomerEntity || $customer->getGuest() === false) {
return;
}
$customerData = null;
if (\array_key_exists('advancedMatching', $config) && $config['advancedMatching'] === true) {
$criteria = new Criteria();
$criteria->addAssociation('salutation');
$criteria->addAssociation('defaultShippingAddress.country');
$criteria->addAssociation('defaultShippingAddress.countryState');
$criteria->addAssociation('defaultShippingAddress.salutation');
$criteria->addFilter(new EqualsFilter('id', $customer->getId()));
$customer = $this->customerRepository->search($criteria, $event->getContext())->first();
if ($customer instanceof CustomerEntity) {
$customerData = PrepareCustomerData::handle($customer);
}
}
$dataStruct = new DataStruct();
$dataStruct->assign([
'customer' => $customerData,
'order' => PrepareOrder::handle($order),
]);
$event->getPage()->addExtension('mediameetsFbPixel', $dataStruct);
}
}