<?php
declare(strict_types=1);
namespace MadSearchPrice\Subscriber;
use MadBodymedBundle\Service\BundleConfigurationLoader;
use MadFastBuy\Storefront\Page\FastBuy\FastBuyPageLoader;
use Shopware\Core\Content\Product\Cart\ProductGatewayInterface;
use Shopware\Core\Content\Product\Events\ProductSuggestResultEvent;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayEntity;
class SearchSubscriber implements EventSubscriberInterface
{
private SalesChannelRepositoryInterface $salesChannelProductRepository;
public function __construct(
SalesChannelRepositoryInterface $salesChannelProductRepository
) {
$this->salesChannelProductRepository = $salesChannelProductRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductSearchResultEvent::class => 'onProductListingResult',
ProductSuggestResultEvent::class => 'onProductListingResult',
];
}
public function onProductListingResult(ProductListingResultEvent $event): void
{
foreach ($event->getResult() as $product) {
$data['firstPrice'] = 0;
$data['lastPrice'] = 0;
$data['hasPriceRange'] = false;
if($product->getParentId()){
$criteria = new Criteria([$product->getParentId()]);
$criteria->addAssociation('children.options');
$criteria->addAssociation('children.prices');
$criteria->addAssociation('children.deliveryTime');
$mainProduct = $this->salesChannelProductRepository->search(
$criteria,
$event->getSalesChannelContext()
)->getEntities()->first();
if($mainProduct->getChildren()){
foreach($mainProduct->getChildren() as $childProduct) {
if ($childProduct->getCalculatedPrices()->getElements()) {
if ($data['firstPrice'] == 0 && $data['lastPrice'] == 0) {
$data['firstPrice'] = $childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice();
$data['lastPrice'] = $childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice();
} else {
if ($childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice() < $data['firstPrice']) {
$data['firstPrice'] = $childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice();
}
if ($childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice() > $data['lastPrice']) {
$data['lastPrice'] = $childProduct->getCalculatedPrices()->getElements()[0]->getUnitPrice();
}
}
}elseif($childProduct->getCalculatedPrice()){
if ($data['firstPrice'] == 0 && $data['lastPrice'] == 0) {
$data['firstPrice'] = $childProduct->getCalculatedPrice()->getUnitPrice();
$data['lastPrice'] = $childProduct->getCalculatedPrice()->getUnitPrice();
} else {
if ($childProduct->getCalculatedPrice()->getUnitPrice() < $data['firstPrice']) {
$data['firstPrice'] = $childProduct->getCalculatedPrice()->getUnitPrice();
}
if ($childProduct->getCalculatedPrice()->getUnitPrice() > $data['lastPrice']) {
$data['lastPrice'] = $childProduct->getCalculatedPrice()->getUnitPrice();
}
}
}
}
}
if($data['firstPrice'] != $data['lastPrice']){
$data['hasPriceRange'] = true;
}
$prices = new ArrayEntity(['prices' => $data]);
$product->addExtension('searchPriceFrom', $prices);
}
}
}
}