custom/plugins/HuebertAccessoriesDirectly/src/Subscriber/CrossSellingSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Huebert\AccessoriesDirectly\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Storefront\Page\Product\Configurator\ProductPageConfiguratorLoader;
  8. use Shopware\Storefront\Page\Product\CrossSelling\CrossSellingLoadedEvent;
  9. use Shopware\Storefront\Page\Product\CrossSelling\CrossSellingProductCriteriaEvent;
  10. use Shopware\Core\Content\Product\Events\ProductCrossSellingsLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CrossSellingSubscriber implements EventSubscriberInterface
  13. {
  14.     protected $optionRepository;
  15.     /**
  16.      * @var ProductPageConfiguratorLoader
  17.      */
  18.     private $configuratorLoader;
  19.     public function __construct(EntityRepositoryInterface $optionRepositoryProductPageConfiguratorLoader $configuratorLoader)
  20.     {
  21.         $this->optionRepository $optionRepository;
  22.         $this->configuratorLoader $configuratorLoader;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             CrossSellingLoadedEvent::class => "onCrossSellingLoaded",
  28.             ProductCrossSellingsLoadedEvent::class => "onProductCrossSellingLoaded"
  29.         ];
  30.     }
  31.     //legacy function for < 6.3.2.0
  32.     public function onCrossSellingLoaded(CrossSellingLoadedEvent $crossSellingLoadedEvent)
  33.     {
  34.         $elements $crossSellingLoadedEvent->getCrossSellingResult()->getElements();
  35.         foreach ($elements as $element) {
  36.             $products $element->getProducts();
  37.             foreach ($products as $product) {
  38.                 if ($product->getOptionIds()) {
  39.                     $parts = [];
  40.                     foreach ($product->getOptionIds() as $optionId) {
  41.                         $criteria = New Criteria();
  42.                         $criteria
  43.                             ->addFilter(new EqualsFilter('id'$optionId))
  44.                             ->addAssociation('group');
  45.                         $option $this->optionRepository->search($criteria$crossSellingLoadedEvent->getContext())->first();
  46.                         $parts[] = [
  47.                             'group' => $option->getGroup()->getTranslation('name'),
  48.                             'option' => $option->getTranslation('name')
  49.                         ];
  50.                     }
  51.                     $product->setVariation(
  52.                         $parts
  53.                     );
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     public function onProductCrossSellingLoaded(ProductCrossSellingsLoadedEvent $crossSellingLoadedEvent)
  59.     {
  60.         $elements $crossSellingLoadedEvent->getCrossSellings();
  61.         foreach ($elements as $element) {
  62.             $products $element->getProducts();
  63.             foreach ($products as $product) {
  64.                 $_productConfigurator $this->configuratorLoader->load($product$crossSellingLoadedEvent->getSalesChannelContext());
  65.                 if ($product->getOptionIds() && count($product->getVariation()) === 0) {
  66.                     $parts = [];
  67.                     foreach ($product->getOptionIds() as $optionId) {
  68.                         $criteria = New Criteria();
  69.                         $criteria
  70.                             ->addFilter(new EqualsFilter('id'$optionId))
  71.                             ->addAssociation('group');
  72.                         $option $this->optionRepository->search($criteria$crossSellingLoadedEvent->getContext())->first();
  73.                         if ($option) {
  74.                             $groupName null;
  75.                             if ($option->getGroup()) {
  76.                                 if ($option->getGroup()->getTranslation('name')) {
  77.                                     $groupName $option->getGroup()->getTranslation('name');
  78.                                 }
  79.                             }
  80.                             $optionName null;
  81.                             if ($option->getTranslation('name')) {
  82.                                 $optionName $option->getTranslation('name');
  83.                             }
  84.                             $parts[] = [
  85.                                 'group' => $groupName,
  86.                                 'option' => $optionName
  87.                             ];
  88.                         }
  89.                     }
  90.                     if (count($parts) > 0) {
  91.                         $product->setVariation(
  92.                             $parts
  93.                         );
  94.                     }
  95.                 }
  96.                 $product->assign(
  97.                   [  'productConfigurator' => $_productConfigurator,]
  98.                 );
  99.             }
  100.         }
  101.     }
  102. }