custom/plugins/MaxiaListingVariants6/src/Core/Content/Product/Cms/ProductBoxCmsElementResolver.php line 95

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\FieldConfig;
  12. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
  13. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductBoxStruct;
  14. use Shopware\Core\Content\Product\ProductDefinition;
  15. use Shopware\Core\Content\Product\ProductEntity;
  16. class ProductBoxCmsElementResolver extends AbstractCmsElementResolver
  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->buildCriteria($request$resolverContext->getSalesChannelContext());
  67.             $criteriaCollection->add('product_' $slot->getUniqueIdentifier(), ProductDefinition::class, $criteria);
  68.             $config $slot->getFieldConfig();
  69.             $config->set('product', new FieldConfig(
  70.                 'product',
  71.                 FieldConfig::SOURCE_STATIC,
  72.                 $criteria->getFilters()[0]->getValue()
  73.             ));
  74.             return $criteriaCollection;
  75.         }
  76.         return $this->parent->collect($slot$resolverContext);
  77.     }
  78.     public function enrich(CmsSlotEntity $slotResolverContext $resolverContextElementDataCollection $result): void
  79.     {
  80.         $this->parent->enrich($slot$resolverContext$result);
  81.         $config $this->configService->getBaseConfig($resolverContext->getSalesChannelContext());
  82.         if (!$config->isPluginEnabled()) {
  83.             return;
  84.         }
  85.         /** @var ProductBoxStruct $productBox */
  86.         $productBox $slot->getData();
  87.         if (!$productBox instanceof ProductBoxStruct) return;
  88.         $product $productBox->getProduct();
  89.         if (!$product instanceof ProductEntity) return;
  90.         $config $this->configService->getProductConfig($product$slot$resolverContext->getSalesChannelContext());
  91.         $product->addExtension('maxiaListingVariants'$config);
  92.         try {
  93.             if (!$product->getParentId() && $product->getChildCount()) {
  94.                 // avoid issues when main product is overridden with the preselected variant
  95.                 $product = clone $product;
  96.             }
  97.             $this->listingVariantsLoader->load([$product], $resolverContext->getSalesChannelContext());
  98.             $productBox->setProduct($product);
  99.             $productBox->setProductId($product->getId());
  100.         } catch (\Exception $e) {
  101.             $this->logger->error('Error when loading variants in ProductBoxCmsElementResolver: '.
  102.                 $e->getMessage().",\nStack trace: ".$e->getTraceAsString());
  103.         }
  104.     }
  105. }