custom/plugins/LinkMobilityShopwareSmsApiPlugin/src/Subscriber/CheckoutCompleteSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace LinkMobility\ShopwareSmsApiPlugin\Subscriber;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedCriteriaEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class CheckoutCompleteSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private RequestStack $requestStack, private EntityRepository $customerRepository)
  11.     {
  12.     }
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             CheckoutOrderPlacedCriteriaEvent::class => 'updateCustomer',
  17.             ];
  18.     }
  19.     public function updateCustomer(CheckoutOrderPlacedCriteriaEvent $event): void
  20.     {
  21.         $consentInput $this->requestStack->getCurrentRequest()?->request?->get('link_mobility_shopware_sms_api_plugin_consent');
  22.         if (null === $consentInput) {
  23.             return;
  24.         }
  25.         $consent 'on' === $consentInput;
  26.         $salesChannelContext $event->getSalesChannelContext();
  27.         $this->customerRepository->update(
  28.             [
  29.                 [
  30.                     'id' => $salesChannelContext->getCustomer()?->getId(),
  31.                     'customFields' => [
  32.                         'link_mobility_shopware_sms_api_plugin_consent' => $consent,
  33.                     ],
  34.                 ],
  35.             ],
  36.             $salesChannelContext->getContext()
  37.         );
  38.     }
  39. }