<?php
declare(strict_types = 1);
namespace GrimmTheme\Storefront\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
class CartSubscriber implements EventSubscriberInterface {
private $productRepository;
public function __construct(
EntityRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents()
{
return [
CheckoutCartPageLoadedEvent::class => 'onCartPageLoadedEvent',
OffcanvasCartPageLoadedEvent::class => 'asdf'
];
}
public function asdf(OffcanvasCartPageLoadedEvent $event) {
$this->determineIfCrossSelling($event);
}
public function onCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event) {
$this->determineIfCrossSelling($event);
}
public function determineIfCrossSelling(PageLoadedEvent $event) {
$cart = $event->getPage()->getCart();
$lineItemIds = [];
foreach($cart->getLineItems() as $lineItem) {
if($lineItem->getType() === CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE) {
$filteredArray = $lineItem->getChildren()->filterFlatByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
if(count($filteredArray) > 0) {
$lineItemIds[] = $filteredArray[0]->getReferencedId();
}
}
if($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
$lineItemIds[] = $lineItem->getReferencedId();
}
}
$productsWithCrossSelling = [];
foreach($this->getShit($lineItemIds, $event) as $product) {
if(count($product->getCrossSellings()) > 0) {
$productsWithCrossSelling[] = $product->getId();
}
}
foreach($cart->getLineItems() as $lineItem) {
if($lineItem->getType() === CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE) {
$filteredArray = $lineItem->getChildren()->filterFlatByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
if(count($filteredArray) > 0) {
$productChild = $filteredArray[0];
if(in_array($productChild->getReferencedId(), $productsWithCrossSelling)) {
$lineItem->addExtension('s360_crossSelling', new ArrayStruct([true]));
}
}
}
if($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
if(in_array($lineItem->getId(), $productsWithCrossSelling)) {
$lineItem->addExtension('s360_crossSelling', new ArrayStruct([true]));
}
}
}
}
private function getShit(Array $ids, PageLoadedEvent $event) {
$criteria = (new Criteria())
->addFilter(new EqualsAnyFilter('id', $ids))
->addAssociation('crossSellings');
// EntitySearchResult
return $this->productRepository->search($criteria, $event->getContext());
}
}