custom/plugins/KsAdditions/src/Subscribers/ProductSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace Ks\KsAdditions\Subscribers;
  3. use Ks\KsAdditions\KsAdditions;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MinAggregation;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  10. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductSubscriber implements EventSubscriberInterface
  13. {
  14.     private EntityRepositoryInterface $customFieldRepository;
  15.     private EntityRepositoryInterface $productRepository;
  16.     public function __construct(
  17.         EntityRepositoryInterface $customFieldRepository,
  18.         EntityRepositoryInterface $productRepository
  19.     ) {
  20.         $this->customFieldRepository $customFieldRepository;
  21.         $this->productRepository $productRepository;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ProductPageLoadedEvent::class => ['onProductPageLoaded'1]
  27.         ];
  28.     }
  29.     /**
  30.      * @param ProductPageLoadedEvent $event
  31.      */
  32.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  33.     {
  34.         $product $event->getPage()->getProduct();
  35.         if ($product->getParentId()) {
  36.             $criteria = new Criteria();
  37.             $criteria
  38.                 ->addFilter(new EqualsFilter('product.parentId'$product->getParentId()))
  39.                 ->addFilter(new EqualsFilter('product.options.groupId''8cedf9077a45432f9e26b43379f6bd37'));
  40.             $criteria
  41.                 ->addAssociation('options')
  42.                 ->addAssociation('options.group');
  43.             $criteria->addAggregation(
  44.                 new MinAggregation('min-fitting''product.customFields.' KsAdditions::ADDVISOR_PASSUNG_MIN),
  45.                 new MaxAggregation('max-fitting''product.customFields.' KsAdditions::ADDVISOR_PASSUNG_MAX),
  46.             );
  47.             $criteria->addSorting(new FieldSorting('product.stock'FieldSorting::DESCENDING));
  48.             $variants $this->productRepository->search($criteria$event->getContext());
  49.             $productVariantOptions = [];
  50.             foreach ($variants as $variant) {
  51.                 foreach ($variant->getOptions() as $option) {
  52.                     if (
  53.                         $option->getGroupId() == '8cedf9077a45432f9e26b43379f6bd37'
  54.                         and !isset($productVariantOptions[$option->getGroupId() . '-' $option->getId()])
  55.                     ) {
  56.                         if (isset($variant->getCustomFields()['addvisor_passung_min']) || isset($variant->getCustomFields()['addvisor_passung_max'])) {
  57.                             $productVariantOptions[$option->getGroupId() . '-' $option->getId()] = [
  58.                                 'groupId' => $option->getGroupId(),
  59.                                 'optionId' => $option->getId(),
  60.                                 'title' => $option->getName(),
  61.                                 'fittingMin' => $variant->getCustomFields()['addvisor_passung_min'],
  62.                                 'fittingMax' => $variant->getCustomFields()['addvisor_passung_max'],
  63.                                 //'stock' => $variant->getStock(),
  64.                             ];
  65.                         } else {
  66.                             return;
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.             /*usort($productVariantOptions, function ($a, $b) {
  72.                 return $b['stock'] - $a['stock'];
  73.             });*/
  74.             $event->getPage()->getProduct()->assign(
  75.                 [
  76.                     'ksProductFitting' => [
  77.                         'min' => $variants->getAggregations()->get('min-fitting')->getVars()['min'],
  78.                         'max' => $variants->getAggregations()->get('max-fitting')->getVars()['max'],
  79.                         'variants' => $productVariantOptions
  80.                     ]
  81.                 ]
  82.             );
  83.         }
  84.     }
  85. }