Search code examples
phploopsshopwareshopware6

Condition loop by entities & values in Shopware 6


I try to condition loop by entities & values in Shopware 6. I use it to filter options. I understand, that I do condition wrong, but I can't get how to do it right with Entities and Values.

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

        $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;
            }

            // To get Tags only for currentPropertyOptions I try to set a condition:
            if (!in_array($options, $currentPropertyOptions)) {
                continue;
            }
            
            $tags->merge($extension);
        }
        $event->getContext()->addExtension('myTags1', $tags);

        // Tags values of a currentPropertyOptions should be delivered in an array
        // $tags = ['Tag1value', 'Tag2value', 'Tag3value', 'Tag4value'];

        if (in_array('3f777000a2734deead391133cee3a6a9', $currentPropertyOptions)) {
            //Scenario1

            $multiFilter = new MultiFilter(MultiFilter::CONNECTION_OR);

            foreach ($tags as $tag) {
                $multiFilter->addQuery(new PrefixFilter('product.properties.name', $tag));
            }

            $criteria->addFilter($multiFilter);
        }
        ...

I have an empty result then:

Shopware\Core\System\Tag\TagCollection {#8107 ▼
  #elements: []
  #extensions: []
}

Do someone know, how this should be done?


Solution

  • This won't work as expected:

    !in_array($options, $currentPropertyOptions)
    

    I assume $currentPropertyOptions is an array of ids. You won't find these in an entity collection like that.

    You can set the ids as filter on the criteria:

    $criteria1 = new Criteria();
    $criteria1->addAssociation('myTags');
    $criteria1->setIds($currentPropertyOptions);