custom/plugins/ProcWegmannTheme/src/Subscriber/CustomerLoginSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace Proclane\WegmannTheme\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  9. use Shopware\Core\System\SalesChannel\SalesChannel\SalesChannelContextSwitcher;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CustomerLoginSubscriber implements EventSubscriberInterface
  13. {
  14.     private const CUSTOM_ZLOTY 'PLN';
  15.     private const ZLOTY_ISO_CODE 'PLN';
  16.     private const CUSTOM_POUND 'GBP';
  17.     private const POUND_ISO_CODE 'GBP';
  18.     public function __construct(
  19.         private readonly SalesChannelContextSwitcher $contextSwitcher,
  20.         private readonly EntityRepositoryInterface $currencyRepository,
  21.     ) {
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CustomerLoginEvent::class => 'onCustomerLoginEvent'
  27.         ];
  28.     }
  29.     public function onCustomerLoginEvent(CustomerLoginEvent $event): void
  30.     {
  31.         $salesChannelContext $event->getSalesChannelContext();
  32.         /**
  33.          * If currency customField is set, retrieve the corresponding currency end update the context.
  34.          * If the customField is not set, ignore.
  35.          */
  36.         if ($customer_currency $event->getCustomer()?->getCustomFields()["additional_custom_customer_currency"] ?? null) {
  37.             if ($currency_id $this->getCurrencyId($customer_currency$salesChannelContext)) {
  38.                 $this->contextSwitcher->update(
  39.                     new DataBag(["currencyId" => $currency_id]),
  40.                     $salesChannelContext
  41.                 );
  42.             }
  43.         } /**
  44.          * If customField is not set and the current currency is not the default, set back to default.
  45.          * This happens if the customField was removed by from the customer after login.
  46.          */
  47.         else { 
  48.            
  49.             $this->contextSwitcher->update(
  50.                 new DataBag(["currencyId" => $salesChannelContext->getSalesChannel()->getCurrencyId()]),
  51.                 $salesChannelContext
  52.             );
  53.             
  54.         }
  55.     }
  56.     private function getCurrencyId(string $isoSalesChannelContext $context): string|null
  57.     {
  58.         $criteria = new Criteria();
  59.         // Using a switch in case more currencies could be added to the list in the future - Easy implementation
  60.         switch ($iso) {
  61.             case self::CUSTOM_ZLOTY:
  62.                 $criteria->addFilter(new EqualsFilter('isoCode'self::ZLOTY_ISO_CODE));
  63.                 break;
  64.             case self::CUSTOM_POUND:
  65.                 $criteria->addFilter(new EqualsFilter('isoCode'self::POUND_ISO_CODE));
  66.                 break;
  67.             default:
  68.                 // Get default currency (EUR)
  69.                 return $context->getSalesChannel()->getCurrencyId();
  70.         }
  71.         return $this->currencyRepository->searchIds($criteria$context->getContext())->firstId();
  72.     }
  73. }