<?php declare(strict_types=1);
namespace ShopStudio\PixelYourShop\EventSubscriber;
use ShopStudio\PixelYourShop\View\TransientBag\Transient;
use ShopStudio\PixelYourShop\View\TransientBag\TransientBagInterface;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemRemovedEvent;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @since 1.0.0
*/
class CheckoutCartEventSubscriber implements EventSubscriberInterface
{
/**
* @since 1.0.0
*/
protected TransientBagInterface $transientBag;
/**
* @since 2.0.0
*/
public function __construct(TransientBagInterface $transientBag)
{
$this->transientBag = $transientBag;
}
/**
* @since 1.0.0
*
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
AfterLineItemAddedEvent::class => 'onAfterLineItemAddedEvent',
AfterLineItemRemovedEvent::class => 'onAfterLineItemRemovedEvent',
];
}
/**
* @since 2.0.0
*/
public function onAfterLineItemAddedEvent(AfterLineItemAddedEvent $event): void
{
$lineItem = $event->getLineItems()[0] ?? null;
if ($lineItem !== null && $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
$this->transientBag->set(new Transient('add_to_cart', $lineItem));
}
}
/**
* @since 2.6.1
*/
public function onAfterLineItemRemovedEvent(AfterLineItemRemovedEvent $event): void
{
$lineItem = $event->getLineItems()[0] ?? null;
if ($lineItem !== null && $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
$this->transientBag->set(new Transient('remove_from_cart', $lineItem));
}
}
}