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

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 'customer_currency_zloty';
  15.     private const ZLOTY_ISO_CODE 'PLN';
  16.     public function __construct(
  17.         private readonly SalesChannelContextSwitcher $contextSwitcher,
  18.         private readonly EntityRepositoryInterface $currencyRepository,
  19.     ) {
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             CustomerLoginEvent::class => 'onCustomerLoginEvent'
  25.         ];
  26.     }
  27.     public function onCustomerLoginEvent(CustomerLoginEvent $event): void
  28.     {
  29.         $salesChannelContext $event->getSalesChannelContext();
  30.         /**
  31.          * If currency customField is set, retrieve the corresponding currency end update the context.
  32.          * If the customField is not set, ignore.
  33.          */
  34.         if ($customer_currency $event->getCustomer()?->getCustomFields()["additional_custom_customer_currency"]) {
  35.             if ($currency_id $this->getCurrencyId($customer_currency$salesChannelContext)) {
  36.                 $this->contextSwitcher->update(
  37.                     new DataBag(["currencyId" => $currency_id]),
  38.                     $salesChannelContext
  39.                 );
  40.             }
  41.         } /**
  42.          * If customField is not set and the current currency is not the default, set back to default.
  43.          * This happens if the customField was removed by from the customer after login.
  44.          */
  45.         elseif ($salesChannelContext->getSalesChannel()->getCurrencyId() !== $event->getContext()->getCurrencyId()) {
  46.             if ($currency_id $this->getCurrencyId($salesChannelContext->getCurrency()->getIsoCode(),
  47.                 $salesChannelContext)) {
  48.                 $this->contextSwitcher->update(
  49.                     new DataBag(["currencyId" => $currency_id]),
  50.                     $salesChannelContext
  51.                 );
  52.             }
  53.         }
  54.     }
  55.     private function getCurrencyId(string $isoSalesChannelContext $context): string|null
  56.     {
  57.         $criteria = new Criteria();
  58.         // Using a switch in case more currencies could be added to the list in the future - Easy implementation
  59.         switch ($iso) {
  60.             case self::CUSTOM_ZLOTY:
  61.                 $criteria->addFilter(new EqualsFilter('isoCode'self::ZLOTY_ISO_CODE));
  62.                 break;
  63.             default:
  64.                 // Get default currency (EUR)
  65.                 return $context->getSalesChannel()->getCurrencyId();
  66.         }
  67.         return $this->currencyRepository->searchIds($criteria$context->getContext())->firstId();
  68.     }
  69. }