<?php declare(strict_types=1);
namespace EconsorBundles\Subscriber;
use EconsorBundles\Services\BundleService;
use EconsorBundles\Subscriber\Struct\ColorStruct;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\Struct;
use Shopware\Core\System\System;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class BundleSubscriber implements EventSubscriberInterface
{
/**
* @var CartService
*/
private $cartService;
/**
* @var BundleService
*/
private $bundleService;
/**
* @var SessionInterface
*/
private $session;
/**
* @var EntityRepositoryInterface
*/
private $ecBundlesRepository;
/**
* @var EntityRepositoryInterface
*/
private $propertyGroupRepository;
/**
* @var SystemConfigService
*/
private $systemConfigService;
public function __construct(CartService $cartService, BundleService $bundleService, SessionInterface $session, EntityRepositoryInterface $ecBundlesRepository, EntityRepositoryInterface $propertyGroupRepository, SystemConfigService $systemConfigService)
{
$this->cartService = $cartService;
$this->bundleService = $bundleService;
$this->session = $session;
$this->ecBundlesRepository = $ecBundlesRepository;
$this->propertyGroupRepository = $propertyGroupRepository;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
BeforeLineItemAddedEvent::class => 'denyQuantityChange',
CustomerLoginEvent::class => 'changeSessionId',
OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartLoaded',
CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoaded'
];
}
public function denyQuantityChange(BeforeLineItemAddedEvent $event)
{
if ('econsorbundle' === $event->getLineItem()->getType()) {
$bundleLineItems = $event->getCart()->getLineItems()->filterType('econsorbundle');
foreach ($bundleLineItems as $bundleLineItem) {
if ($bundleLineItem->getReferencedId() === $event->getLineItem()->getReferencedId()) {
$event->getCart()->getLineItems()->get($bundleLineItem->getReferencedId())->setQuantity(1);
} else {
$event->getCart()->getLineItems()->removeElement($bundleLineItem);
}
}
}
}
public function changeSessionId(CustomerLoginEvent $event) :void
{
$bundleLineItems = $this->cartService->getCart(
$event->getContextToken(),
$event->getSalesChannelContext()
)->getLineItems()->filterType('econsorbundle');
foreach ($bundleLineItems as $bundleLineItem) {
if ($this->bundleService->hasKey($event->getSalesChannelContext(), $bundleLineItem->getId())) {
$this->ecBundlesRepository->update(
[
[
'id' => $bundleLineItem->getId(),
'token' => $event->getContextToken(),
'sessionId' => $this->session->getId()
]
],
$event->getContext()
);
}
}
}
public function onOffcanvasCartLoaded(OffcanvasCartPageLoadedEvent $event)
{
$this->getColorGroup($event);
}
public function onCheckoutCartPageLoaded(CheckoutCartPageLoadedEvent $event)
{
$this->getColorGroup($event);
}
public function getColorGroup($event)
{
$colorId = $this->systemConfigService->get('EconsorBundles.config.ecBundlesColor');
$propertyGroup = $this->propertyGroupRepository->search(new Criteria([$colorId]), $event->getContext())->getEntities();
$colorGroup = $propertyGroup->get($colorId)->getTranslated()['name'];
$colorStruct = new ColorStruct();
$colorStruct->setColorGroup($colorGroup);
$event->getPage()->addExtension('colorId', $colorStruct);
}
}