<?php
declare(strict_types=1);
namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
use BartCPreemiumArticle\PremiumArticle\Repository\MinPriceRepositoryInterface;
use BartCPreemiumArticle\PremiumArticle\Repository\TotalPriceRepositoryInterface;
use BartCPreemiumArticle\PremiumArticle\Service\CheckIfCustomerGroupHasAnyPremiumArticleInterface;
use BartCPreemiumArticle\PremiumArticle\Service\FindPremiumProductsInCartInterface;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ShowCanvasButton implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
private FindPremiumProductsInCartInterface $findPremiumProducts;
private MinPriceRepositoryInterface $minPrice;
private TotalPriceRepositoryInterface $totalPrice;
private CheckIfCustomerGroupHasAnyPremiumArticleInterface $checkIfCustomerGroupHasAnyPremiumArticle;
public function __construct(
SystemConfigService $systemConfigService,
FindPremiumProductsInCartInterface $findPremiumProducts,
MinPriceRepositoryInterface $minPrice,
TotalPriceRepositoryInterface $totalPrice,
CheckIfCustomerGroupHasAnyPremiumArticleInterface $checkIfCustomerGroupHasAnyPremiumArticle
) {
$this->systemConfigService = $systemConfigService;
$this->findPremiumProducts = $findPremiumProducts;
$this->minPrice = $minPrice;
$this->totalPrice = $totalPrice;
$this->checkIfCustomerGroupHasAnyPremiumArticle = $checkIfCustomerGroupHasAnyPremiumArticle;
}
public static function getSubscribedEvents(): array
{
return [
OffcanvasCartPageLoadedEvent::class => 'onCanvaLoad',
];
}
public function onCanvaLoad(OffcanvasCartPageLoadedEvent $event): void
{
$cart = $event->getPage()->getCart();
$multiAdd = $this->systemConfigService->get(
'BartCPreemiumArticle.config.multiadd',
$event->getSalesChannelContext()->getSalesChannel()->getId()
);
$multiAddMax = $this->systemConfigService->get(
'BartCPreemiumArticle.config.multiaddMax',
$event->getSalesChannelContext()->getSalesChannel()->getId()
);
$totalPrice = $this->totalPrice->get($cart, $event->getSalesChannelContext());
$products = $this->findPremiumProducts->find($cart);
$minPrice = $this->minPrice->get($products);
$isSomePremiumArticleInCurrentCustomerGroup
= $this->checkIfCustomerGroupHasAnyPremiumArticle->get(
$event->getSalesChannelContext()->getCustomer(),
$event->getSalesChannelContext()
);
$event->getPage()->addExtension(
'view',
new ArrayEntity(
[
'show_button_in_canvas' => $this->systemConfigService->get(
'BartCPreemiumArticle.config.showbuttonincanvascart',
$event->getSalesChannelContext()->getSalesChannel()->getId()
),
'isSomePremiumArticleInCurrentCustomerGroup' => $isSomePremiumArticleInCurrentCustomerGroup,
'total_price' => $totalPrice,
'multiAdd' => $multiAdd,
'multiAddMax' => $multiAddMax,
'minPrice' => $minPrice,
]
)
);
}
}