<?php declare(strict_types=1);
namespace DonCarneTheme\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use DonCarneTheme\Service\GraphQLClient;
use Shopware\Storefront\Checkout\Cart\SalesChannel\StorefrontCartFacade;
use DonCarneTheme\Service\BeefClubService;
class CustomerLoginSubscriber implements EventSubscriberInterface
{
private StorefrontCartFacade $cartFacade;
private BeefClubService $beefClubService;
public function __construct(
StorefrontCartFacade $cartFacade,
BeefClubService $beefClubService
) {
$this->cartFacade = $cartFacade;
$this->beefClubService = $beefClubService;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
CustomerLoginEvent::class => 'onCustomerLogin'
];
}
public function onCustomerLogin(CustomerLoginEvent $event)
{
$customer = $event->getCustomer();
$token = $event->getContextToken();
$cartBeforeNewContext = $this->cartFacade->get($token, $event->getSalesChannelContext());
$beefClubInCart = $this->beefClubService->checkBeefClubProductAlreadyInCart($cartBeforeNewContext);
// If customer has a valid beef club subscription or customer has beef club product in cart
if ($this->beefClubService->customerhasMembership($customer) || $beefClubInCart) {
$this->beefClubService->changeCustomerGroupToBeefClub($customer);
} else {
$this->beefClubService->changeCustomerGroupToStandard($customer);
}
}
}