custom/plugins/MaxiaListingVariants6/src/Core/Content/Product/Cms/ProductListingCmsElementResolver.php line 79

Open in your IDE?
  1. <?php
  2. namespace Maxia\MaxiaListingVariants6\Core\Content\Product\Cms;
  3. use Maxia\MaxiaListingVariants6\Service\ConfigService;
  4. use Maxia\MaxiaListingVariants6\Service\ListingVariantsLoader;
  5. use Monolog\Logger;
  6. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  7. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  8. use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
  9. use Shopware\Core\Content\Cms\DataResolver\Element\CmsElementResolverInterface;
  10. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  11. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  12. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductListingStruct;
  13. use Shopware\Core\Content\Product\ProductEntity;
  14. use Shopware\Core\Content\Product\SalesChannel\Listing\AbstractProductListingRoute;
  15. use Shopware\Core\Framework\Uuid\Uuid;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class ProductListingCmsElementResolver extends AbstractCmsElementResolver
  19. {
  20.     /**
  21.      * @var Logger
  22.      */
  23.     protected $logger;
  24.     /**
  25.      * @var CmsElementResolverInterface
  26.      */
  27.     protected $parent;
  28.     /**
  29.      * @var AbstractProductListingRoute
  30.      */
  31.     protected $listingRoute;
  32.     /**
  33.      * @var ListingVariantsLoader
  34.      */
  35.     protected $listingVariantsLoader;
  36.     /**
  37.      * @var ConfigService
  38.      */
  39.     protected $configService;
  40.     /**
  41.      * @param Logger $logger
  42.      * @param CmsElementResolverInterface $parent
  43.      * @param AbstractProductListingRoute $listingRoute
  44.      * @param ListingVariantsLoader $variantListingService
  45.      * @param ConfigService $configService
  46.      */
  47.     public function __construct(
  48.         Logger $logger,
  49.         CmsElementResolverInterface $parent,
  50.         AbstractProductListingRoute $listingRoute,
  51.         ListingVariantsLoader $variantListingService,
  52.         ConfigService $configService
  53.     ) {
  54.         $this->logger $logger;
  55.         $this->parent $parent;
  56.         $this->listingRoute $listingRoute;
  57.         $this->listingVariantsLoader $variantListingService;
  58.         $this->configService $configService;
  59.     }
  60.     public function getType(): string
  61.     {
  62.         return $this->parent->getType();
  63.     }
  64.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  65.     {
  66.         return $this->parent->collect($slot$resolverContext);
  67.     }
  68.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  69.     {
  70.         $config $this->configService->getBaseConfig($resolverContext->getSalesChannelContext());
  71.         if (!$config->isPluginEnabled()) {
  72.             $this->parent->enrich($slot$resolverContext$result);
  73.             return;
  74.         }
  75.         $request $resolverContext->getRequest();
  76.         $context $resolverContext->getSalesChannelContext();
  77.         $route $request->attributes->get('_route');
  78.         if ($route === 'frontend.plugins.maxia-listing-variants.product-box') {
  79.             // build different criteria on selection change
  80.             $data = new ProductListingStruct();
  81.             $navigationId $this->getNavigationId($request$context);
  82.             $criteria $this->listingVariantsLoader->buildCriteria($request$resolverContext->getSalesChannelContext());
  83.             $criteria->setTitle('cms::product-listing');
  84.             if (class_exists('\Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingCollection')) {
  85.                 $request->request->set('order''id');
  86.                 $criteria->addExtension('sortings'self::createSortings());
  87.             }
  88.             $listing $this->listingRoute
  89.                 ->load($navigationId$request$context$criteria)
  90.                 ->getResult();
  91.             $data->setListing($listing);
  92.             $slot->setData($data);
  93.         } else {
  94.             $this->parent->enrich($slot$resolverContext$result);
  95.         }
  96.         // load variants data
  97.         /** @var ProductListingStruct $listing */
  98.         $listing $slot->getData();
  99.         $products $listing->getListing()->getEntities();
  100.         if (!$products || !$products->count()) {
  101.             return;
  102.         }
  103.         foreach ($products->getElements() as $product) {
  104.             /** @var ProductEntity $product */
  105.             $config $this->configService->getProductConfig($product$slot$resolverContext->getSalesChannelContext());
  106.             $product->addExtension('maxiaListingVariants'$config);
  107.         }
  108.         try {
  109.             $this->listingVariantsLoader->load($products->getElements(), $resolverContext->getSalesChannelContext());
  110.         } catch (\Exception $e) {
  111.             $this->logger->error('Error when loading variants in ProductListingCmsElementResolver: '.
  112.                 $e->getMessage().",\nStack trace: ".$e->getTraceAsString());
  113.         }
  114.     }
  115.     private function getNavigationId(Request $requestSalesChannelContext $salesChannelContext): string
  116.     {
  117.         $navigationId $request->attributes
  118.             ->get('navigationId'$salesChannelContext->getSalesChannel()->getNavigationCategoryId());
  119.         if ($navigationId) {
  120.             return $navigationId;
  121.         }
  122.         $params $request->attributes->get('_route_params');
  123.         if ($params && isset($params['navigationId'])) {
  124.             return $params['navigationId'];
  125.         }
  126.         return $salesChannelContext->getSalesChannel()->getNavigationCategoryId();
  127.     }
  128.     public static function createSortings()
  129.     {
  130.         $productSortingEntity = new \Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingEntity();
  131.         $productSortingEntity->setId(Uuid::randomHex());
  132.         $productSortingEntity->setActive(true);
  133.         $productSortingEntity->setTranslated(['label' => 'Default']);
  134.         $productSortingEntity->setKey('id');
  135.         $productSortingEntity->setPriority(5);
  136.         $productSortingEntity->setFields([
  137.             [
  138.                 'field' => 'product.id',
  139.                 'order' => 'desc',
  140.                 'priority' => 1,
  141.                 'naturalSorting' => 0,
  142.             ],
  143.         ]);
  144.         $collection = new \Shopware\Core\Content\Product\SalesChannel\Sorting\ProductSortingCollection();
  145.         $collection->add($productSortingEntity);
  146.         return $collection;
  147.     }
  148. }