<?php
declare(strict_types=1);
namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
use BartCPreemiumArticle\PremiumArticle\Service\ProductHelper;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RemovePreemiumArticleFromCart implements EventSubscriberInterface
{
private ProductHelper $checkIfPremiumArticleSell;
public function __construct(
ProductHelper $checkIfPremiumArticleSell
) {
$this->checkIfPremiumArticleSell = $checkIfPremiumArticleSell;
}
public static function getSubscribedEvents(): array
{
return [
BeforeLineItemAddedEvent::class => 'onProductAdded',
];
}
//todo Czy to się kiedyś wydarzy? Klasa raczej do usunięcia
public function onProductAdded(BeforeLineItemAddedEvent $event): void
{
$cart = $event->getCart();
$product = $event->getLineItem();
if (strpos($product->getId(), 'premiumarticle') === false && $product->getType() === 'product') {
$sell = $this->checkIfPremiumArticleSell->checkIfSellAvailable($product->getId());
if (!$sell && $cart->has($product->getId())) {
$extension = $product->getExtension('view');
if ($extension === null || !isset($extension->getVars()['data']['premiumProduct'])
|| $extension->getVars()['data']['premiumProduct'] === false) {
$cart->remove($product->getId());
}
}
}
}
}