<?php
declare(strict_types=1);
namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
use BartCPreemiumArticle\PremiumArticle\Service\ProductHelper;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RemoveBuyButton implements EventSubscriberInterface
{
private ProductHelper $productHelper;
public function __construct(ProductHelper $productHelper)
{
$this->productHelper = $productHelper;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoad',
];
}
public function onProductPageLoad(ProductPageLoadedEvent $event): void
{
$page = $event->getPage();
$productId = $page->getProduct()->getId();
if ($page->getProduct()->getParentId()) {
$sellAvailable = false;
if ($this->productHelper->checkIfSellAvailable($page->getProduct()->getId())
&& $this->productHelper->checkIfSellAvailable($page->getProduct()->getParentId())
) {
$sellAvailable = true;
}
} else {
$sellAvailable = $this->productHelper->checkIfSellAvailable($productId);
}
$event->getPage()->addExtension(
'view',
new ArrayEntity(
[
'sellAvailable' => $sellAvailable,
]
)
);
}
}