After Shopware update 6.5.5.2 => 6.5.8.11 custom filter plugin doesn’t work. It should filter properties depending on another property selected.
Here it is how Subscriber.php class looks:
class Subscriber implements EventSubscriberInterface
{
private EntityRepository $optionRepository;
public function __construct(EntityRepository $optionRepository)
{
$this->optionRepository = $optionRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductListingCriteriaEvent::class => [
['onListingCriteria', -200],
],
];
}
public function onListingCriteria(ProductListingCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('properties');
$event->getCriteria()->addAssociation('properties.group');
$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();
$event->getContext()->addExtension('currentPropertyOptionsCriteria', new ArrayEntity($currentPropertyOptions));
$criteria1 = new Criteria();
$criteria1->addAssociation('myTags');
$criteria1->setIds($currentPropertyOptions);
$tags = new TagCollection();
$options = $this->optionRepository->search($criteria1, $event->getContext())->getEntities();
/** @var PropertyGroupOptionEntity $option */
foreach ($options as $option) {
$extension = $option->getExtension('myTags');
if (!$extension instanceof TagCollection) {
continue;
}
$tags->merge($extension);
}
$tags = $option->getExtension('myTags');
$tagnames = $tags->map(fn (TagEntity $tag) => $tag->getName());
$event->getContext()->addExtension('myTagsNames1', new ArrayEntity($tagnames));
$prefixes = ['84a5f522d04f463a93a858b3c1bb8f7a','40f24f88c4c34196ad21ec5dcd094e6a', 'e892c99ffc5747848aea3aa5c8126201', '96e839a6e32a4301b238e90088f4a778', 'c5d7bcee60f7493bb6203965e15068e0', '9c493945d37b4ff1b527a874d8315519', 'f032b2d21c484196ad3a98d90379f6c0', 'a07ec367574a4c3d81b7fb6ceae66ffe'];
if (in_array('3f777000a2734deead391133cee3a6a9', $currentPropertyOptions)) {
$multiFilter = new MultiFilter(MultiFilter::CONNECTION_OR);
foreach ($tagnames as $tagname) { $multiFilter->addQuery(new PrefixFilter('product.properties.name', $tagname));}
foreach ($prefixes as $prefix) {
$multiFilter->addQuery(new EqualsFilter('product.properties.group.id', $prefix));
}
$criteria->addFilter($multiFilter);
}
..
if (in_array('a637fe94fe794a84b0ce7fc2a13e1485', $currentPropertyOptions)) {
$multiFilter = new MultiFilter(MultiFilter::CONNECTION_OR);
foreach ($tagnames as $tagname) { $multiFilter->addQuery(new PrefixFilter('product.properties.name', $tagname));}
foreach ($prefixes as $prefix) {
$multiFilter->addQuery(new EqualsFilter('product.properties.group.id', $prefix));
}
$criteria->addFilter($multiFilter);
}
}
}
I try to debug it, but can’t find an issue. Associated Properties Tags are in place. But it doesn’t filter properties depending on selected one.
Can someone give a tip on debug? Or maybe there is a new way of implementing this kind of custom filtering in Shopware 6.5.8.11 or 6.6 (we are going to update to latest version)?