custom/plugins/MoorlCustomerAccounts/src/Subscriber/StorefrontSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlCustomerAccounts\Subscriber;
  3. use MoorlCustomerAccounts\Core\Content\CustomerAccountStruct;
  4. use MoorlCustomerAccounts\Core\Service\CustomerAccountService;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  7. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  8. use Shopware\Core\System\SalesChannel\Event\SalesChannelProcessCriteriaEvent;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class StorefrontSubscriber implements EventSubscriberInterface
  12. {
  13.     private CustomerAccountService $customerAccountService;
  14.     public function __construct(
  15.         CustomerAccountService $customerAccountService
  16.     )
  17.     {
  18.         $this->customerAccountService $customerAccountService;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             CustomerLoginEvent::class => 'onCustomerLogin',
  24.             SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolved',
  25.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  26.             'sales_channel.product.process.criteria' => [
  27.                 ['onProductCriteria'10],
  28.             ]
  29.         ];
  30.     }
  31.     public function onProductCriteria(SalesChannelProcessCriteriaEvent $event): void
  32.     {
  33.         $this->enrichCustomer($event->getSalesChannelContext());
  34.     }
  35.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  36.     {
  37.         $salesChannelContext $this->customerAccountService->getSalesChannelContext();
  38.         if (!$salesChannelContext) {
  39.             return;
  40.         }
  41.         $this->customerAccountService->addCustomerIdToOrder($event->getOrder());
  42.         $customer $salesChannelContext->getCustomer();
  43.         /* @var $customerAccount CustomerAccountStruct */
  44.         $customerAccount $customer->getExtension('CustomerAccount');
  45.         if ($customerAccount && $customerAccount->getOrderCopy()) {
  46.             $email $customerAccount->getParent()->getEmail();
  47.             $recipents $event->getMailStruct()->getRecipients();
  48.             $recipents[$email] = $email;
  49.             $event->getMailStruct()->setRecipients($recipents);
  50.             $event->getMailStruct()->setCc($email); // Not working for the moment
  51.         }
  52.     }
  53.     public function onSalesChannelContextResolved(SalesChannelContextResolvedEvent $event): void
  54.     {
  55.         $this->enrichCustomer($event->getSalesChannelContext());
  56.     }
  57.     public function onCustomerLogin(CustomerLoginEvent $event): void
  58.     {
  59.         // TODO: If parent removed, throw error
  60.     }
  61.     private function enrichCustomer(SalesChannelContext $salesChannelContext): void
  62.     {
  63.         $customer $salesChannelContext->getCustomer();
  64.         if (!$customer || $customer->hasExtension('CustomerAccount')) {
  65.             return;
  66.         }
  67.         $this->customerAccountService->setSalesChannelContext($salesChannelContext);
  68.         $customerAccount = new CustomerAccountStruct();
  69.         $customFields $customer->getCustomFields();
  70.         if ($customFields && !empty($customFields['moorl_ca_parent_id'])) {
  71.             $parent $this->customerAccountService->getCustomer($customFields['moorl_ca_parent_id'], false);
  72.             $this->customerAccountService->syncCustomer($customer$parent);
  73.             $customerAccount->setParent($parent);
  74.             $customerAccount->setOrderCopy(!empty($customFields['moorl_ca_order_copy']));
  75.             $customer->setId($parent->getId());
  76.         } else {
  77.             $customerAccount->setChildren($this->customerAccountService->getCustomers());
  78.         }
  79.         $customer->addExtension('CustomerAccount'$customerAccount);
  80.     }
  81. }