custom/plugins/MadSearchPrice/src/Subscriber/SearchSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadSearchPrice\Subscriber;
  4. use MadBodymedBundle\Service\BundleConfigurationLoader;
  5. use MadFastBuy\Storefront\Page\FastBuy\FastBuyPageLoader;
  6. use Shopware\Core\Content\Product\Cart\ProductGatewayInterface;
  7. use Shopware\Core\Content\Product\Events\ProductSuggestResultEvent;
  8. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  9. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  10. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\Framework\Struct\ArrayEntity;
  16. class SearchSubscriber implements EventSubscriberInterface
  17. {
  18.     private SalesChannelRepositoryInterface $salesChannelProductRepository;
  19.     public function __construct(
  20.         SalesChannelRepositoryInterface $salesChannelProductRepository
  21.     ) {
  22.         $this->salesChannelProductRepository $salesChannelProductRepository;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ProductSearchResultEvent::class => 'onProductListingResult',
  28.             ProductSuggestResultEvent::class => 'onProductListingResult',
  29.         ];
  30.     }
  31.     public function onProductListingResult(ProductListingResultEvent $event): void
  32.     {
  33.         foreach ($event->getResult() as $product) {
  34.             $data['firstPrice'] = 0;
  35.             $data['lastPrice'] = 0;
  36.             $data['hasPriceRange'] = false;
  37.             if($product->getParentId()){
  38.                 $criteria = new Criteria([$product->getParentId()]);
  39.                 $criteria->addAssociation('children.options');
  40.                 $criteria->addAssociation('children.prices');
  41.                 $criteria->addAssociation('children.deliveryTime');
  42.                 $mainProduct $this->salesChannelProductRepository->search(
  43.                     $criteria,
  44.                     $event->getSalesChannelContext()
  45.                 )->getEntities()->first();
  46.                 if($mainProduct->getChildren()){
  47.                     foreach($mainProduct->getChildren() as $childProduct) {
  48.                         if ($childProduct->getCalculatedPrices()->getElements()) {
  49.                                 if ($data['firstPrice'] == && $data['lastPrice'] == 0) {
  50.                                     $data['firstPrice'] = $childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice();
  51.                                     $data['lastPrice'] = $childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice();
  52.                                 } else {
  53.                                     if ($childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice() < $data['firstPrice']) {
  54.                                         $data['firstPrice'] = $childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice();
  55.                                     }
  56.                                     if ($childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice() > $data['lastPrice']) {
  57.                                         $data['lastPrice'] = $childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice();
  58.                                     }
  59.                                 }
  60.                         }elseif($childProduct->getCalculatedPrice()){
  61.                             if ($data['firstPrice'] == && $data['lastPrice'] == 0) {
  62.                             $data['firstPrice'] = $childProduct->getCalculatedPrice()->getUnitPrice();
  63.                             $data['lastPrice'] = $childProduct->getCalculatedPrice()->getUnitPrice();
  64.                             } else {
  65.                                 if ($childProduct->getCalculatedPrice()->getUnitPrice() < $data['firstPrice']) {
  66.                                     $data['firstPrice'] = $childProduct->getCalculatedPrice()->getUnitPrice();
  67.                                 }
  68.                                 if ($childProduct->getCalculatedPrice()->getUnitPrice() > $data['lastPrice']) {
  69.                                     $data['lastPrice'] = $childProduct->getCalculatedPrice()->getUnitPrice();
  70.                                 }
  71.                             }
  72.                         }
  73.                     }
  74.                 }
  75.                 if($data['firstPrice'] != $data['lastPrice']){
  76.                     $data['hasPriceRange'] = true;
  77.                 }
  78.                 $prices = new ArrayEntity(['prices' => $data]);
  79.                 $product->addExtension('searchPriceFrom'$prices);
  80.             }
  81.         }
  82.     }
  83. }