<?php
declare(strict_types=1);
namespace VioCustomerPrice\Subscriber;
use Shopware\Core\Content\Product\Events\ProductCrossSellingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductCrossSellingStreamCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductGatewayCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductListingPreviewCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\QuickView\MinimalQuickViewPageCriteriaEvent;
use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
use Shopware\Storefront\Pagelet\Wishlist\GuestWishListPageletProductCriteriaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductCriteriaEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductGatewayCriteriaEvent::class => 'onProductGatewayCriteria',
ProductCrossSellingCriteriaEvent::class => 'onProductCrossSellingCriteriaEvent',
ProductCrossSellingStreamCriteriaEvent::class => 'onProductCrossSellingCriteriaEvent',
ProductListingCriteriaEvent::class => 'onProductListingCriteriaEvent',
ProductListingPreviewCriteriaEvent::class => 'onProductListingPreviewCriteriaEvent',
ProductSearchCriteriaEvent::class => 'onProductListingCriteriaEvent',
ProductSuggestCriteriaEvent::class => 'onProductListingCriteriaEvent',
ProductPageCriteriaEvent::class => 'onProductPageCriteriaEvent',
WishListPageProductCriteriaEvent::class => 'onWishListPageProductCriteriaEvent',
GuestWishListPageletProductCriteriaEvent::class => 'onGuestWishListPageletProductCriteriaEvent',
MinimalQuickViewPageCriteriaEvent::class => 'onMinimalQuickViewPageCriteriaEvent',
];
}
public function onMinimalQuickViewPageCriteriaEvent(MinimalQuickViewPageCriteriaEvent $event): void
{
$this->addCustomerPricesAssociation(
$event->getCriteria(),
$event->getSalesChannelContext()
);
}
public function onGuestWishListPageletProductCriteriaEvent(GuestWishListPageletProductCriteriaEvent $event): void
{
$this->addCustomerPricesAssociation(
$event->getCriteria(),
$event->getSalesChannelContext()
);
}
public function onWishListPageProductCriteriaEvent(WishListPageProductCriteriaEvent $event): void
{
$this->addCustomerPricesAssociation(
$event->getCriteria(),
$event->getSalesChannelContext()
);
}
public function onProductPageCriteriaEvent(ProductPageCriteriaEvent $event): void
{
$this->addCustomerPricesAssociation(
$event->getCriteria(),
$event->getSalesChannelContext()
);
}
public function onProductListingPreviewCriteriaEvent(ProductListingPreviewCriteriaEvent $event): void
{
$this->addCustomerPricesAssociation(
$event->getCriteria(),
$event->getSalesChannelContext()
);
}
public function onProductCrossSellingCriteriaEvent(ProductCrossSellingCriteriaEvent $event): void
{
$this->addCustomerPricesAssociation(
$event->getCriteria(),
$event->getSalesChannelContext()
);
}
public function onProductListingCriteriaEvent(ProductListingCriteriaEvent $event): void
{
$this->addCustomerPricesAssociation(
$event->getCriteria(),
$event->getSalesChannelContext()
);
}
public function onProductGatewayCriteria(ProductGatewayCriteriaEvent $event): void
{
$this->addCustomerPricesAssociation(
$event->getCriteria(),
$event->getSalesChannelContext()
);
}
protected function addCustomerPricesAssociation(Criteria $criteria, SalesChannelContext $context): void
{
if($context->getCustomer()) {
$criteria->getAssociation('customerPrices')
->addFilter(new EqualsFilter('customerId', $context->getCustomer()->getId()))
->addSorting(new FieldSorting('quantityStart'));
}
}
}