How can I create filter with customfields?
I update the Shopware 5 to Shopware 6 and some of my fields into customfields (eg: migration_ComuMess5_product_series, migration_ComuMess5_product_author, migration_ComuMess5_product_company ...)
How can I create filter with customfields?
I update the Shopware 5 to Shopware 6 and some of my fields into customfields (eg: migration_ComuMess5_product_series, migration_ComuMess5_product_author, migration_ComuMess5_product_company ...)
It's because I didn't say it clearly. I have created a filter with customfields bool but I am having difficulty creating a filter with string, which displays as mutiselect. Here is my code:
Subscriber/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);
}
}
services.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 %}
Bug: EntityRepository for entity "product_migration_ComuMess5_product_series" does not exist.
I want create a filter based on my customfield "migration_ComuMess5_product_series"
Here is another code that worked:
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
} %}
The difference between them is that one is a bool, the other is a string.
There is a page in the documentation about creating custom filters for the storefront. Please take a look: https://developer.shopware.com/docs/guides/plugins/plugins/storefront/add-listing-filters.html