<?php
declare(strict_types=1);
namespace LinkMobility\ShopwareSmsApiPlugin\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedCriteriaEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CheckoutCompleteSubscriber implements EventSubscriberInterface
{
public function __construct(private RequestStack $requestStack, private EntityRepository $customerRepository)
{
}
public static function getSubscribedEvents()
{
return [
CheckoutOrderPlacedCriteriaEvent::class => 'updateCustomer',
];
}
public function updateCustomer(CheckoutOrderPlacedCriteriaEvent $event): void
{
$consentInput = $this->requestStack->getCurrentRequest()?->request?->get('link_mobility_shopware_sms_api_plugin_consent');
if (null === $consentInput) {
return;
}
$consent = 'on' === $consentInput;
$salesChannelContext = $event->getSalesChannelContext();
$this->customerRepository->update(
[
[
'id' => $salesChannelContext->getCustomer()?->getId(),
'customFields' => [
'link_mobility_shopware_sms_api_plugin_consent' => $consent,
],
],
],
$salesChannelContext->getContext()
);
}
}