custom/plugins/MaxiaListingVariants6/src/Core/Content/Product/Cms/CrossSellingCmsElementResolver.php line 99

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\CmsElementResolverInterface;
  9. use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
  10. use Shopware\Core\Content\Cms\DataResolver\FieldConfig;
  11. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  12. use Shopware\Core\Content\Cms\SalesChannel\Struct\CrossSellingStruct;
  13. use Shopware\Core\Content\Product\Cms\AbstractProductDetailCmsElementResolver;
  14. use Shopware\Core\Content\Product\ProductDefinition;
  15. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CrossSellingElement;
  16. class CrossSellingCmsElementResolver extends AbstractProductDetailCmsElementResolver
  17. {
  18.     /**
  19.      * @var Logger
  20.      */
  21.     protected $logger;
  22.     /**
  23.      * @var CmsElementResolverInterface
  24.      */
  25.     protected $parent;
  26.     /**
  27.      * @var ListingVariantsLoader
  28.      */
  29.     protected $listingVariantsLoader;
  30.     /**
  31.      * @var ConfigService
  32.      */
  33.     protected $configService;
  34.     /**
  35.      * @param Logger $logger
  36.      * @param CmsElementResolverInterface $parent
  37.      * @param ListingVariantsLoader $variantListingService
  38.      * @param ConfigService $configService
  39.      */
  40.     public function __construct(
  41.         Logger $logger,
  42.         CmsElementResolverInterface $parent,
  43.         ListingVariantsLoader $variantListingService,
  44.         ConfigService $configService
  45.     ) {
  46.         $this->logger $logger;
  47.         $this->parent $parent;
  48.         $this->listingVariantsLoader $variantListingService;
  49.         $this->configService $configService;
  50.     }
  51.     public function getType(): string
  52.     {
  53.         return $this->parent->getType();
  54.     }
  55.     public function collect(CmsSlotEntity $slotResolverContext $resolverContext): ?CriteriaCollection
  56.     {
  57.         $config $this->configService->getBaseConfig($resolverContext->getSalesChannelContext());
  58.         if (!$config->isPluginEnabled()) {
  59.             return $this->parent->collect($slot$resolverContext);
  60.         }
  61.         $request $resolverContext->getRequest();
  62.         $route $request->attributes->get('_route');
  63.         if ($route === 'frontend.plugins.maxia-listing-variants.product-box') {
  64.             // build different criteria on selection change
  65.             $criteriaCollection = new CriteriaCollection();
  66.             $criteria $this->listingVariantsLoader->buildSwitcherCriteria(
  67.                 $request,
  68.                 $resolverContext->getSalesChannelContext(),
  69.                 false
  70.             );
  71.             $criteriaCollection->add('product_' $slot->getUniqueIdentifier(), ProductDefinition::class, $criteria);
  72.             $config $slot->getFieldConfig();
  73.             $config->set('product', new FieldConfig(
  74.                 'product',
  75.                 FieldConfig::SOURCE_STATIC,
  76.                 $criteria->getFilters()[0]->getValue()
  77.             ));
  78.             return $criteriaCollection;
  79.         }
  80.         return $this->parent->collect($slot$resolverContext);
  81.     }
  82.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  83.     {
  84.         $this->parent->enrich($slot$resolverContext$result);
  85.         $config $this->configService->getBaseConfig($resolverContext->getSalesChannelContext());
  86.         if (!$config->isPluginEnabled() || !$config->isShowInCrossSelling()) {
  87.             return;
  88.         }
  89.         /** @var CrossSellingStruct $struct */
  90.         $struct $slot->getData();
  91.         if (!$struct instanceof CrossSellingStruct) return;
  92.         $crossSellings $struct->getCrossSellings();
  93.         if (!$crossSellings || !$crossSellings->count()) return;
  94.         $products = [];
  95.         foreach ($crossSellings as $crossSelling) {
  96.             /** @var $crossSelling CrossSellingElement */
  97.             foreach ($crossSelling->getProducts() as $product) {
  98.                 if ($product->hasExtension('maxiaListingVariants')) {
  99.                     continue;
  100.                 }
  101.                 $config $this->configService->getProductConfig($product$slot$resolverContext->getSalesChannelContext());
  102.                 $product->addExtension('maxiaListingVariants'$config);
  103.                 $products[] = $product;
  104.             }
  105.         }
  106.         try {
  107.             $this->listingVariantsLoader->load($products$resolverContext->getSalesChannelContext());
  108.         } catch (\Exception $e) {
  109.             $this->logger->error('Error when loading variants in CrossSellingCmsElementResolver: '.
  110.                 $e->getMessage().",\nStack trace: ".$e->getTraceAsString());
  111.         }
  112.     }
  113. }