custom/plugins/CompraEsiSW6/src/Core/Checkout/Customer/Subscriber/CustomerFlowEventsSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\EsiSW6\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Event\CustomerChangedPaymentMethodEvent;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  5. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  6. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  9. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextRestorer;
  10. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  11. class CustomerFlowEventsSubscriber extends \Shopware\Core\Checkout\Customer\Subscriber\CustomerFlowEventsSubscriber
  12. {
  13.     private EventDispatcherInterface $dispatcher;
  14.     private SalesChannelContextRestorer $restorer;
  15.     public function __construct(
  16.         EventDispatcherInterface $dispatcher,
  17.         SalesChannelContextRestorer $restorer
  18.     ) {
  19.         $this->dispatcher $dispatcher;
  20.         $this->restorer $restorer;
  21.         parent::__construct(
  22.             $dispatcher,
  23.             $restorer
  24.         );
  25.     }
  26.     public function onCustomerWritten(EntityWrittenEvent $event): void
  27.     {
  28.         if ($event->getContext()->getSource() instanceof SalesChannelApiSource) {
  29.             return;
  30.         }
  31.         $payloads $event->getPayloads();
  32.         foreach ($payloads as $payload) {
  33.             // COMPRA change: check for CustomField "compra_is_esi_source" and source of event
  34.             $customFields $payload['customFields'] ?? [];
  35.             $isEsiSource array_key_exists('compra_is_esi_source'$customFields) ? $customFields['compra_is_esi_source'] : false;
  36.             $isAdminApiSource = ($event->getContext()->getSource() instanceof AdminApiSource);
  37.             /*
  38.              * Adapt the original logic and add additional condition:
  39.              * Only dispatch to CustomerChangePaymentMethodEvent if
  40.              * - "compra_is_esi_source" isn't set to true OR
  41.              * - source of event is not an AdminApiSource
  42.              */
  43.             if (!empty($payload['defaultPaymentMethodId']) && empty($payload['createdAt'])
  44.                 && (!$isEsiSource || !$isAdminApiSource)
  45.             ) {
  46.                 $this->dispatchCustomerChangePaymentMethodEvent($payload['id'], $event);
  47.                 continue;
  48.             }
  49.             /*
  50.              * Adapt the original logic and add additional condition:
  51.              * Only dispatch to CustomerRegisterEvent if
  52.              * - "compra_is_esi_source" isn't set to true OR
  53.              * - source of event is not an AdminApiSource
  54.              */
  55.             if (!empty($payload['createdAt'])
  56.                 && (!$isEsiSource || !$isAdminApiSource)
  57.             ) {
  58.                 $this->dispatchCustomerRegisterEvent($payload['id'], $event);
  59.             }
  60.         }
  61.     }
  62.     private function dispatchCustomerRegisterEvent(string $customerIdEntityWrittenEvent $event): void
  63.     {
  64.         $context $event->getContext();
  65.         $salesChannelContext $this->restorer->restoreByCustomer($customerId$context);
  66.         if (!$customer $salesChannelContext->getCustomer()) {
  67.             return;
  68.         }
  69.         $customerCreated = new CustomerRegisterEvent(
  70.             $salesChannelContext,
  71.             $customer
  72.         );
  73.         $this->dispatcher->dispatch($customerCreated);
  74.     }
  75.     private function dispatchCustomerChangePaymentMethodEvent(string $customerIdEntityWrittenEvent $event): void
  76.     {
  77.         $context $event->getContext();
  78.         $salesChannelContext $this->restorer->restoreByCustomer($customerId$context);
  79.         if (!$customer $salesChannelContext->getCustomer()) {
  80.             return;
  81.         }
  82.         $customerChangePaymentMethodEvent = new CustomerChangedPaymentMethodEvent(
  83.             $salesChannelContext,
  84.             $customer,
  85.             new RequestDataBag()
  86.         );
  87.         $this->dispatcher->dispatch($customerChangePaymentMethodEvent);
  88.     }
  89. }