custom/plugins/cutvertCrosssellingCart/src/Subscriber/Subscriber.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace cutvert\CrosssellingCart\Subscriber;
  3. use Petstore30\controllers\Store;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  7. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\AbstractProductCrossSellingRoute;
  8. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Event\StorefrontRenderEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Shopware\Core\Content\Product\ProductEvents;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class Subscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var SystemConfigService
  20.      */
  21.     private $systemConfigService;
  22.     private AbstractProductCrossSellingRoute $crossSellingRoute;
  23.     private CartService $cartService;
  24.     public function __construct(SystemConfigService $systemConfigServiceAbstractProductCrossSellingRoute $crossSellingRouteCartService $cartService)
  25.     {
  26.         $this->systemConfigService $systemConfigService;
  27.         $this->crossSellingRoute $crossSellingRoute;
  28.         $this->cartService $cartService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded',
  34.             StorefrontRenderEvent::class => 'onStorefrontRender'
  35.         ];
  36.     }
  37.     public function onProductsLoaded(EntityLoadedEvent $event): void
  38.     {
  39.         $this->PreviewSettings $this->systemConfigService->get('cutvertCrosssellingCart.config');
  40.     }
  41.     public function onStorefrontRender(StorefrontRenderEvent $event){
  42.         $products = [];
  43.         $cart $this->cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext());
  44.         /** @var LineItem $lineItem */
  45.         $lineItems $cart->getLineItems()->filterFlatByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  46.         foreach($lineItems as $lineItem){
  47.             $productId $lineItem->getReferencedId();
  48.             $crossSellingProducts $this->crossSellingRoute->load($productId, new Request(), $event->getSalesChannelContext(), new Criteria());
  49.             /** @var SalesChannelProductEntity $object */
  50.             foreach ($crossSellingProducts->getObject() as $object){
  51.                 foreach ($object->getProducts() as $product){
  52.                     $products[$product->getId()] = $product;
  53.                 }
  54.             }
  55.         }
  56.         $event->setParameter('cutvertCartCrossselling',$products);
  57.     }
  58. }