symfony 在Shopware 6中使用ListingResult getCurrentFilter('properties ')过滤ListingCriteria

62lalag4  于 2023-05-18  发布在  PWA
关注(0)|答案(1)|浏览(173)

如何使它工作,以便我可以根据getCurrentFilter('properties ')的结果过滤条件?
以便所选特性可以包括在条件中。现在由于事件顺序的原因它不起作用。
我可以告诉Symfony重新加载页面来获取它吗?
有没有人知道另一个事件来实现这一点?

<?php // declare(strict_types=1);

namespace CustomFilterBasedOnSelectedOptions\Subscriber;

class Subscriber implements EventSubscriberInterface
{
    private ?array $currentPropertyOptions = null;

    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingCriteriaEvent::class => 'ListingCriteria',
            ProductListingResultEvent::class => 'ListingResult'
        ];
    }

        public function ListingResult(ProductListingResultEvent $event)
    {
        $properties = $event->getResult();
        $this->currentPropertyOptions = $properties->getCurrentFilter('properties');
    }

    public function ListingCriteria(ProductListingCriteriaEvent $event): void
   {
        $event->getCriteria()->addAssociation('properties');
        $event->getCriteria()->addAssociation('properties.group');

        $currentPropertyOptions = $this->currentPropertyOptions;

        /*
        if (in_array('c0d02d1738fd4293a489695787e06b5c', $currentPropertyOptions)) {
            $criteria = $event->getCriteria();

            $criteria->addFilter(new MultiFilter(
                MultiFilter::CONNECTION_OR,
                    [
                        new ContainsFilter('product.properties.name', 'Option1'),
                        new ContainsFilter('product.properties.name', 'Option2')
                    ]
                )
            );
        }
        */
   }
}
laik7k3q

laik7k3q1#

如果我的假设是正确的,那么您不需要一个属性来在两个事件之间存储过滤器值。构建过滤器时,它们被存储为Criteria的扩展。您只需要以足够低的优先级订阅ProductListingCriteriaEvent,这样在侦听器被调用时扩展就已经设置好了。通过将优先级设置为-200,它应该足够低。
然后,只需检查是否设置了带有FilterCollection的扩展名,并包括带有properties键的Filter,以提取过滤器值。

use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;
use Shopware\Core\Content\Product\SalesChannel\Listing\FilterCollection;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class Subscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingCriteriaEvent::class => [
                ['onListingCriteria', -200],
            ],
        ];
    }

    public function onListingCriteria(ProductListingCriteriaEvent $event): void
    {
        $criteria = $event->getCriteria();
        $filters = $criteria->getExtension('filters');

        if (!$filters instanceof FilterCollection) {
            return;
        }

        $propertyFilter = $filters->get('properties');

        if (!$propertyFilter instanceof Filter || !\is_array($propertyFilter->getValues())) {
            return;
        }

        $currentPropertyOptions = $propertyFilter->getValues();

        // ...
    }
}

相关问题