<?php declare(strict_types=1);
namespace Expert\AutobatMarktThemePlugin\Subscriber;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
class FrontendListingSubscriber implements EventSubscriberInterface
{
const EXCLUDE_SORTING = ['price-desc','price-asc','angebot','name-desc','name-asc'];
public static function getSubscribedEvents()
{
return [
ProductListingCriteriaEvent::class => ['handleListingRequestBefore', 500],
ProductSearchCriteriaEvent::class => ['handleListingRequestBefore', 500]
];
}
public function handleListingRequestBefore(ProductListingCriteriaEvent $event): void
{
// add sorted properties group to listing
$event->getCriteria()->addAssociation('properties.group');
// AM-746 Sortierung nicht Verfügbarer Produkte
// AM-775 Sortierungsfunktion Preis Auf,- und Absteigend funktionslos
$request = $event->getRequest();
if(!in_array($request->query->get('order'), self::EXCLUDE_SORTING)) {
$criteria = $event->getCriteria();
$criteria->addSorting(
new FieldSorting('product.available', 'DESC'),
new FieldSorting('product.availableStock', 'DESC'),
new FieldSorting('product.minPurchase', 'DESC'),
);
}
}
}