在 Shopware 6 中使用 ListingResult getCurrentFilter('properties') 筛选 ListingCriteria

Filter ListingCriteria using ListingResult getCurrentFilter('properties') in Shopware 6

提问人:Stacker1427 提问时间:5/17/2023 更新时间:5/17/2023 访问量:81

问:

如何让它工作,以便我可以根据 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')
                    ]
                )
            );
        }
        */
   }
}
PHP symfony 事件 shopware shopware6

评论


答:

1赞 dneustadt 5/17/2023 #1

如果我对你尝试执行的操作的假设是正确的,那么你不需要一个属性来记忆两个事件之间的筛选器值。构造筛选器时,它们将存储为 的扩展名。您只需要以足够低的优先级订阅,因此在调用侦听器时已经设置了分机。通过设置优先级,它应该足够低。CriteriaProductListingCriteriaEvent-200

然后只需检查是否设置了带有 的扩展名并包含 with the key ,以提取过滤器值。FilterCollectionFilterproperties

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();

        // ...
    }
}

评论

0赞 Stacker1427 9/21/2023
您是否知道一些 Shopware 6.5 更新可能会破坏这一点?更新到 6.5 后,我需要手动重新加载页面,无论哪种方式,下拉列表(带有勾选框)都会变得“粘性”。