Shopware 6 如何创建带有自定义字段的过滤器?

Shopware 6 How can make I create filter with custom field?

提问人:Tran Manh 提问时间:11/8/2023 更新时间:11/13/2023 访问量:72

问:

如何使用自定义字段创建过滤器?

我将 Shopware 5 更新为 Shopware 6,并将我的一些字段更新为自定义字段(例如:migration_ComuMess5_product_series、migration_ComuMess5_product_author、migration_ComuMess5_product_company......

Shopware Shopware6-APP

评论


答:

1赞 Ezycod 11/8/2023 #1

文档中有一个关于为店面创建自定义筛选器的页面。 请看一下: https://developer.shopware.com/docs/guides/plugins/plugins/storefront/add-listing-filters.html

评论

0赞 Enigmativity 11/12/2023
此处不鼓励仅链接答案。请在您的答案中发布详细信息,并仅提供支持这些详细信息的链接。链接可能会死掉。
0赞 Tran Manh 11/13/2023 #2

那是因为我没有说清楚。 我已经创建了一个带有customfields bool的过滤器,但是我很难创建一个带有字符串的过滤器,该过滤器显示为mutiselect。 这是我的代码:

订阅者/migration_ComuMess5_product_series.php

<?php

namespace MyFilters\Subscriber;

use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\FilterAggregation;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation;
use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingResult;

class migration_ComuMess5_product_series implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingCollectFilterEvent::class => 'onProductListingCollectFilter',
        ];
    }

    public function onProductListingCollectFilter(ProductListingCollectFilterEvent $event): void
    {
        $filters = $event->getFilters();

        // Your logic to get the filter value
        $ids = $event->getRequest()->get('migration_ComuMess5_product_series');

        // Create an EqualsAnyFilter to check if the value exists in the array
        $equalsAnyFilter = new EqualsFilter(
            'product.customFields.migration_ComuMess5_product_series',
            $ids
        );

        // Wrap it in a Filter object
        $filter = new Filter(
            'filter-migration_ComuMess5_product_series',
            !empty($ids),
            [new EntityAggregation('migration_ComuMess5_product_series', 'product.customFields.migration_ComuMess5_product_series', 'product_migration_ComuMess5_product_series')],
            $equalsAnyFilter,
            $ids
        );

        // Add the filter to the collection
        $filters->add($filter);

    }
}

服务.xml

<service id="MyFilters\Subscriber\migration_ComuMess5_product_series">
    <tag name="kernel.event_subscriber"/>
</service>

filter-panel.html.twig

{% set migration_ComuMess5_product_series = listing.aggregations.get('migration_ComuMess5_product_series') %}
{{ dump(migration_ComuMess5_product_series) }}
{% if migration_ComuMess5_product_series is empty %}
{% set migration_ComuMess5_product_seriesSorted = migration_ComuMess5_product_series.entities|sort((a, b) => a.translated.name|lower <=> b.translated.name|lower) %}
    {% sw_include '@Storefront/storefront/component/listing/filter/filter-multi-select.html.twig' with {
        elements: migration_ComuMess5_product_seriesSorted,
        sidebar: sidebar,
        name: 'migration_ComuMess5_product_series',
        displayName: 'migration_ComuMess5_product_series'|trans|sw_sanitize
    } %}
{% endif %}

错误:实体“product_migration_ComuMess5_product_series”的 EntityRepository 不存在。

评论

0赞 Ezycod 11/13/2023
您声明了“migration_ComuMess5_product_series”作为要在 EntityAggregation 中搜索的实体,但这是您的自定义字段。您要搜索什么,ProductSeries?
0赞 Tran Manh 11/13/2023 #3

我想根据我的自定义字段“migration_ComuMess5_product_series”创建一个过滤器

这是另一个有效的代码:

class migration_ComuMess5_product_neti_purchase_blocker implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [
            ProductListingCollectFilterEvent::class => 'addFilter'
        ];
    }

    public function addFilter(ProductListingCollectFilterEvent $event)
    {
        $filters = $event->getFilters();
        
        $value = (bool) $event->getRequest()->get('migration_ComuMess5_product_neti_purchase_blocker');
       
        $customFieldFilter = new Filter(
            'filter_migration_ComuMess5_product_neti_purchase_blocker',
            $value, 
            [],
            new EqualsFilter('product.customFields.migration_ComuMess5_product_neti_purchase_blocker', $value),
            $value
        );
        
        $filters->add($customFieldFilter);
    }

}

filter-panel.html.twig

{% sw_include '@MyFilters/storefront/component/listing/filter/filter-boolean-customfields.html.twig' with {
    name: 'migration_ComuMess5_product_neti_purchase_blocker',
    displayName: 'product neti purchase blocker'|trans|sw_sanitize
} %}

它们之间的区别在于,一个是布尔值,另一个是字符串。