custom/plugins/FourtwosixRegistrationFields/src/Subscriber/CartConvertedEventSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace FourtwosixRegistrationFields\Subscriber;
  4. use FourtwosixRegistrationFields\FourtwosixRegistrationFields;
  5. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository as CustomerAddressRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. class CartConvertedEventSubscriber implements EventSubscriberInterface
  13. {
  14.     private RequestStack $requestStack;
  15.     private CustomerAddressRepository $addressRepository;
  16.     public function __construct(
  17.         RequestStack $requestStack,
  18.         CustomerAddressRepository $addressRepository
  19.     ) {
  20.         $this->requestStack $requestStack;
  21.         $this->addressRepository $addressRepository;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CartConvertedEvent::class => 'onCartConvertedEvent',
  27.         ];
  28.     }
  29.     public function onCartConvertedEvent(CartConvertedEvent $event): void
  30.     {
  31.         if (
  32.             empty($this->requestStack) ||
  33.             empty($request $this->requestStack->getCurrentRequest())
  34.         ) {
  35.             return;
  36.         }
  37.         $parameters $request->request;
  38.         if (
  39.             !($customer $event->getSalesChannelContext()->getCustomer()) ||
  40.             !($addressBilling $customer->getActiveBillingAddress()) ||
  41.             !($addressShipping $customer->getActiveShippingAddress())
  42.         ) {
  43.             return;
  44.         }
  45.         if (
  46.             ($address $parameters->get('billingAddress')) &&
  47.             ($fiscalCode $address['fiscalCode'])
  48.         ) {
  49.             $this->updateAddress($event->getContext(), $addressBilling$fiscalCode);
  50.         }
  51.         if (
  52.             ($address $parameters->get('shippingAddress')) &&
  53.             ($fiscalCode $address['fiscalCode'])
  54.         ) {
  55.             $this->updateAddress($event->getContext(), $addressShipping$fiscalCode);
  56.         }
  57.     }
  58.     public function updateAddress(Context $contextEntity &$addressstring &$fiscalCode): void
  59.     {
  60.         $this->addCustomField(
  61.             $this->addressRepository,
  62.             $context,
  63.             $address,
  64.             FourtwosixRegistrationFields::FISCAL_CODE_ATTRIBUTE_NAME,
  65.             $fiscalCode
  66.         );
  67.     }
  68.     public function addCustomField(
  69.         EntityRepository $repository,
  70.         Context $context,
  71.         Entity $entity,
  72.         string $name,
  73.         string $value
  74.     ): void {
  75.         $id $entity->getUniqueIdentifier();
  76.         $repository->upsert([[
  77.             'id' => $id,
  78.             'customFields' => [
  79.                 $name => $value
  80.             ]
  81.         ]], $context);
  82.     }
  83. }