custom/plugins/GrimmTheme/src/Storefront/Subscriber/CartSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace GrimmTheme\Storefront\Subscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  6. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  7. use Shopware\Storefront\Page\PageLoadedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Shopware\Core\Framework\Struct\ArrayStruct;
  12. use Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector;
  13. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  14. class CartSubscriber implements EventSubscriberInterface {
  15.     
  16.     private $productRepository;
  17.     public function __construct(
  18.         EntityRepositoryInterface $productRepository
  19.     ) {
  20.         $this->productRepository $productRepository;
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             CheckoutCartPageLoadedEvent::class => 'onCartPageLoadedEvent',
  26.             OffcanvasCartPageLoadedEvent::class => 'asdf'
  27.         ];
  28.     }
  29.     public function asdf(OffcanvasCartPageLoadedEvent $event) {
  30.         $this->determineIfCrossSelling($event);
  31.     }
  32.     public function onCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event) {
  33.         $this->determineIfCrossSelling($event);
  34.     }
  35.     public function determineIfCrossSelling(PageLoadedEvent $event) {
  36.         $cart $event->getPage()->getCart();
  37.         $lineItemIds = [];
  38.         foreach($cart->getLineItems() as $lineItem) {
  39.             if($lineItem->getType() === CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE) {
  40.                 $filteredArray $lineItem->getChildren()->filterFlatByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  41.                 if(count($filteredArray) > 0) {
  42.                     $lineItemIds[] = $filteredArray[0]->getReferencedId();
  43.                 }
  44.             } 
  45.             if($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
  46.                 $lineItemIds[] = $lineItem->getReferencedId();
  47.             }
  48.         }
  49.         
  50.         $productsWithCrossSelling = [];
  51.         foreach($this->getShit($lineItemIds$event) as $product) {
  52.             if(count($product->getCrossSellings()) > 0) {
  53.                 $productsWithCrossSelling[] = $product->getId();
  54.             }
  55.         }
  56.         foreach($cart->getLineItems() as $lineItem) {
  57.             if($lineItem->getType() === CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE) {
  58.                 $filteredArray $lineItem->getChildren()->filterFlatByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  59.                 if(count($filteredArray) > 0) {
  60.                     $productChild $filteredArray[0];
  61.                     if(in_array($productChild->getReferencedId(), $productsWithCrossSelling)) {
  62.                         $lineItem->addExtension('s360_crossSelling', new ArrayStruct([true]));
  63.                     }
  64.                 }
  65.             } 
  66.             if($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
  67.                 if(in_array($lineItem->getId(), $productsWithCrossSelling)) {
  68.                     $lineItem->addExtension('s360_crossSelling', new ArrayStruct([true]));
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     private function getShit(Array $idsPageLoadedEvent $event) {
  74.         $criteria = (new Criteria())
  75.             ->addFilter(new EqualsAnyFilter('id'$ids))
  76.             ->addAssociation('crossSellings');
  77.         // EntitySearchResult
  78.         return $this->productRepository->search($criteria$event->getContext());
  79.     }
  80. }