custom/plugins/theme/src/Subscriber/CustomerLoginSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DonCarneTheme\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  5. use DonCarneTheme\Service\GraphQLClient;
  6. use Shopware\Storefront\Checkout\Cart\SalesChannel\StorefrontCartFacade;
  7. use DonCarneTheme\Service\BeefClubService;
  8. class CustomerLoginSubscriber implements EventSubscriberInterface
  9. {
  10.     private StorefrontCartFacade $cartFacade;
  11.     private BeefClubService $beefClubService;
  12.     public function __construct(
  13.         StorefrontCartFacade $cartFacade,
  14.         BeefClubService $beefClubService
  15.     ) {
  16.         $this->cartFacade $cartFacade;
  17.         $this->beefClubService $beefClubService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  22.         return [
  23.             CustomerLoginEvent::class => 'onCustomerLogin'
  24.         ];
  25.     }
  26.     public function onCustomerLogin(CustomerLoginEvent $event)
  27.     {
  28.         $customer $event->getCustomer();
  29.         $token $event->getContextToken();
  30.         $cartBeforeNewContext $this->cartFacade->get($token$event->getSalesChannelContext());
  31.         $beefClubInCart $this->beefClubService->checkBeefClubProductAlreadyInCart($cartBeforeNewContext);
  32.         // If customer has a valid beef club subscription or customer has beef club product in cart
  33.         if ($this->beefClubService->customerhasMembership($customer) || $beefClubInCart) {
  34.             $this->beefClubService->changeCustomerGroupToBeefClub($customer);
  35.         } else {
  36.             $this->beefClubService->changeCustomerGroupToStandard($customer);
  37.         }
  38.     }
  39. }