custom/plugins/SwpCrosssellingInCartSix/src/Storefront/Subscriber/Frontend.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * Shopware
  4.  * Copyright © 2020
  5.  *
  6.  * @category   Shopware
  7.  * @package    SwpProductOptionsSix
  8.  * @subpackage ProductPageCriteriaSubscriber.php
  9.  *
  10.  * @copyright  2020 Iguana-Labs GmbH
  11.  * @author     Module Factory <info at module-factory.com>
  12.  * @license    https://www.module-factory.com/eula
  13.  */
  14. namespace Swp\CrosssellingInCartSix\Storefront\Subscriber;
  15. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\AbstractProductCrossSellingRoute;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  19. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  20. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class Frontend implements EventSubscriberInterface
  23. {
  24.     /** @var SystemConfigService */
  25.     private $systemConfigService;
  26.     /** @var CachedProductCrossSellingRoute */
  27.     private $crossSellingLoader;
  28.     /** @var CachedProductDetailRoute */
  29.     private $productLoader;
  30.     public function __construct(
  31.         SystemConfigService $systemConfigService,
  32.         AbstractProductCrossSellingRoute $crossSellingLoader,
  33.         AbstractProductDetailRoute $productLoader
  34.     ) {
  35.         $this->systemConfigService $systemConfigService;
  36.         $this->crossSellingLoader $crossSellingLoader;
  37.         $this->productLoader $productLoader;
  38.     }
  39.     /**
  40.      * @return array
  41.      */
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             CheckoutCartPageLoadedEvent::class => 'cartLoaded',
  46.             OffcanvasCartPageLoadedEvent::class => 'canvasCartLoaded'
  47.         ];
  48.     }
  49.     public function canvasCartLoaded(OffcanvasCartPageLoadedEvent $event):void{
  50.         $this->generateCrossSelling($event);
  51.     }
  52.     public function cartLoaded(CheckoutCartPageLoadedEvent $event): void
  53.     {
  54.         $this->generateCrossSelling($event);
  55.     }
  56.     /**
  57.      * @param OffcanvasCartPageLoadedEvent|CheckoutCartPageLoadedEvent $event
  58.      * @return bool
  59.      */
  60.     private function generateCrossSelling($event):bool {
  61.         /** @var array $pluginConfig */
  62.         $pluginConfig $this->systemConfigService->get(
  63.             'SwpCrosssellingInCartSix.config',
  64.             $event->getSalesChannelContext()->getSalesChannel()->getId());
  65.         if ($pluginConfig['channelActive']!=1){
  66.             return false;
  67.         }
  68.         $cart $event->getPage()->getCart()->getLineItems()->getElements();
  69.         $crossSellingProducts = array();
  70.         $limit =intval($pluginConfig['numberOfItems']);
  71.         $limitCount 0;
  72.         $array_keys =array();
  73.         $randomCrossSell = array();
  74.         $criteria = new Criteria();
  75.         foreach ($cart as $key => $val) {
  76.             // check auf valid id
  77.             if (strlen($val->getId()) != 32) {
  78.                 continue;
  79.             }
  80.             $crossSelling $this->crossSellingLoader->load($val->getId(), $event->getRequest(), $event->getSalesChannelContext(), $criteria);
  81.             $crossSellingResult $crossSelling->getResult();
  82.             if(!$crossSellingResult->count()) {
  83.                 continue;
  84.             }
  85.             foreach ($crossSellingResult as $ikey => $ival) {
  86.                 if ($ival->getCrossSelling()->getAssignedProducts() != null) {
  87.                     foreach ($ival->getCrossSelling()->getAssignedProducts()->getElements() as $akey => $aval) {
  88. /*
  89.                         if ($limitCount < $limit) {
  90.                             $product = $this->productLoader->load($aval->getProductId(), $event->getRequest(), $event->getSalesChannelContext(), $criteria);
  91.                             $productData = $product->getProduct();
  92.                             $crossSellingProducts[$productData->getId()] = $productData;
  93.                             $limitCount++;
  94.                         }
  95.                         $array_keys[$aval->getProductId()]=$aval->getProductId();
  96. */
  97.                     }
  98.                 }
  99.                 if ($ival->getProducts() != null) {
  100.                     foreach ($ival->getProducts()->getElements() as $akey => $aval) {
  101.                         if ($limitCount $limit) {
  102.                             $crossSellingProducts[$aval->getId()] = $aval;
  103.                             $limitCount++;
  104.                         }
  105.                         $array_keys[$aval->getId()]=$aval->getId();
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         if(count($array_keys)>&& $pluginConfig['RandomProducts'] == 1) {
  111.             if ($limit count($array_keys)){
  112.                 $limit count($array_keys);
  113.             }
  114.             /** @var array $array_keys */
  115.             $array_keys array_rand($array_keys$limit);
  116.             foreach ($array_keys as $key => $val) {
  117.                 try {
  118.                     $product $this->productLoader->load($val$event->getRequest(), $event->getSalesChannelContext(), $criteria);
  119.                     $productData $product->getProduct();
  120.                     $randomCrossSell[] = $productData;
  121.                 } catch (\Exception $exception) {
  122.                 }
  123.             }
  124.             $crossSellingProducts $randomCrossSell;
  125.         }
  126.         if (count($crossSellingProducts) > 0) {
  127.             $event->getSalesChannelContext()->assign(['CrossSellingInCart' => $crossSellingProducts]);
  128.         }
  129.         return true;
  130.     }
  131. //    /**
  132. //     * @param SalesChannelContext $event
  133. //     * @return array|mixed|null
  134. //     */
  135. //    private function isCannelActive(SalesChannelContext $event)
  136. //    {
  137. //        return $this->systemConfigService->get(
  138. //            'SwpCrosssellingInCartSix.config.channelActive',
  139. //            $event->getSalesChannel()->getId()
  140. //        );
  141. //    }
  142. }