php Shopware 6中的实体和值的条件循环

a14dhokn  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(128)

我尝试在Shopware 6中通过实体和值来实现条件循环。我用它来过滤选项。我明白,我做的条件错误,但我不能得到如何做它的权利与实体和价值观。

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);
        }
        ...

字符串
我得到一个空的结果:

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


有没有人知道,应该怎么做?

xzv2uavs

xzv2uavs1#

这不会像预期的那样工作:

!in_array($options, $currentPropertyOptions)

字符串
我假设$currentPropertyOptions是一个id数组。你不会在这样的实体集合中找到这些。
您可以将id设置为条件上的筛选器:

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

相关问题