<?php declare(strict_types=1);
namespace Samson\Subscriber;
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2020
*
***/
use Samson\Api\TypePlate\Controller\MyProductsController;
use Samson\Service\PersistentCartService;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemQuantityChangedEvent;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class CartSubscriber
* @package Samson\Subscriber
* @author Artur Seitz <artur.seitz@dkd.de>
*/
class CartSubscriber implements EventSubscriberInterface
{
private PersistentCartService $persistentCartService;
public function __construct(PersistentCartService $persistentCartService)
{
$this->persistentCartService = $persistentCartService;
}
public static function getSubscribedEvents(): array
{
return [
BeforeLineItemAddedEvent::class => 'onLineItemAddedEvent',
BeforeLineItemQuantityChangedEvent::class => 'onLineItemQuantityChangedEvent',
BeforeLineItemRemovedEvent::class => 'onLineItemRemovedEvent',
//OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoadedEvent'
];
}
public function onOffcanvasCartPageLoadedEvent(OffcanvasCartPageLoadedEvent $event)
{
if($event->getCart()->getName() === MyProductsController::MY_PRODUCTS_CART_NAME){
return;
}
$cart = $event->getPage()->getCart();
$hideOptions = [
'nicht vorhanden',
'nicht gefordert',
'ohne besondere maßnahmen',
'ohne',
'not required',
'not present',
'without',
'without special instructions',
'-'
];
foreach ($cart->getLineItems() as $lineItem) {
$payloadOptions = [];
$options = $lineItem->getPayload()['options'];
foreach ($options as $option) {
if (!in_array(strtolower($option['option']), $hideOptions)) {
$payloadOptions[] = $option;
}
}
$lineItem->setPayloadValue('options', $payloadOptions);
}
}
public function onLineItemAddedEvent(BeforeLineItemAddedEvent $event): void
{
if($event->getCart()->getName() === MyProductsController::MY_PRODUCTS_CART_NAME){
return;
}
if (empty($event->getSalesChannelContext()->getCustomer())) {
return;
}
$this->persistentCartService->refreshPersistentCart(
$event->getSalesChannelContext()->getCustomer(),
$event->getCart(),
$event->getSalesChannelContext()->getContext()
);
}
public function onLineItemQuantityChangedEvent(BeforeLineItemQuantityChangedEvent $event): void
{
if (empty($event->getSalesChannelContext()->getCustomer())) {
return;
}
$this->persistentCartService->refreshPersistentCart(
$event->getSalesChannelContext()->getCustomer(),
$event->getCart(),
$event->getSalesChannelContext()->getContext()
);
}
public function onLineItemRemovedEvent(BeforeLineItemRemovedEvent $event): void
{
if($event->getCart()->getName() === MyProductsController::MY_PRODUCTS_CART_NAME){
return;
}
if (empty($event->getSalesChannelContext()->getCustomer())) {
return;
}
$this->persistentCartService->refreshPersistentCart(
$event->getSalesChannelContext()->getCustomer(),
$event->getCart(),
$event->getSalesChannelContext()->getContext()
);
}
}