<?php declare(strict_types=1);
namespace Uandi\UandiEfbContactInfo\Subscriber;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Uandi\UandiEfbContactInfo\Service\ContactsService;
class CheckoutFinishSubscriber implements EventSubscriberInterface
{
private ContactsService $contactsService;
public function __construct(ContactsService $contactsService)
{
$this->contactsService = $contactsService;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutFinishPageLoadedEvent::class => 'addContactData'
];
}
/**
* Add contact data to the checkout finish page.
*
* @param CheckoutFinishPageLoadedEvent $event
*
* @return void
*/
public function addContactData(CheckoutFinishPageLoadedEvent $event): void
{
$order = $event->getPage()->getOrder();
$zipCode = $order->getBillingAddress()->getZipcode();
$contactData = $this->contactsService->getContactsByPostCode($event->getContext(), $zipCode, 'dtn');
$struct = new ContactDataStruct();
$struct->setContactData($contactData);
$event->getPage()->addExtension('contact_data', $struct);
}
}