<?php declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 03 lip 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\MojeBambinoEngine\Subscriber;
use Crehler\MojeBambinoEngine\Checkout\Cart\CheckoutCrossSelling\CheckoutCrossSellingBuilder;
use Crehler\MojeBambinoEngine\Service\ProductStatusStorefrontService;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;
class CheckoutCartSubscriber implements EventSubscriberInterface
{
private CheckoutCrossSellingBuilder $checkoutCrossSellingBuilder;
private ProductStatusStorefrontService $productStatus;
private Environment $environment;
public function __construct(
CheckoutCrossSellingBuilder $checkoutCrossSellingBuilder,
ProductStatusStorefrontService $productStatus,
Environment $environment
)
{
$this->checkoutCrossSellingBuilder = $checkoutCrossSellingBuilder;
$this->productStatus = $productStatus;
$this->environment = $environment;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'onCheckoutPageLoaded',
CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPage',
];
}
public function onCheckoutPageLoaded(CheckoutCartPageLoadedEvent $event): void
{
$this->environment->addGlobal('isCheckoutCartPage', true);
$context = $event->getContext();
$page = $event->getPage();
$cart = $page->getCart();
$productId = [];
foreach ($cart->getLineItems() as $key => $lineItem) {
if ($lineItem->getType() !== 'product') {
continue;
}
$productId[] = $lineItem->getReferencedId();
}
$page->addExtension(
'checkoutCrossSellingStream',
$this->checkoutCrossSellingBuilder->buildCheckoutProductStream($productId, $event->getSalesChannelContext()));
foreach ($cart->getLineItems() as $lineItem) {
if ($lineItem->getType() === 'product') {
$lineItem->addExtension(
'productStatus',
$this->productStatus->findProductStatus($lineItem->getReferencedId(), $context)
);
}
}
}
public function onCheckoutConfirmPage(CheckoutConfirmPageLoadedEvent $event): void
{
$context = $event->getContext();
$page = $event->getPage();
$cart = $page->getCart();
foreach ($cart->getLineItems() as $lineItem) {
if ($lineItem->getType() === 'product') {
$lineItem->addExtension(
'productStatus',
$this->productStatus->findProductStatus($lineItem->getReferencedId(), $context)
);
}
}
}
}