条件循环通过Shopware 6中的实体和数值。

huangapple go评论80阅读模式
英文:

Condition loop by entities & values in Shopware 6

问题

我尝试通过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;
        }

        // 为了仅获取currentPropertyOptions的标签,我尝试设置条件:
        if (!in_array($option->getId(), $currentPropertyOptions)) {
            continue;
        }

        $tags->merge($extension);
    }
    $event->getContext()->addExtension('myTags1', $tags);

    // currentPropertyOptions的标签值应以数组形式提供
    // $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 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?

答案1

得分: 1

这不会按预期工作:

!in_array($options, $currentPropertyOptions)

我假设 $currentPropertyOptions 是一个包含 ID 的数组。你不会在实体集合中以这种方式找到它们。

你可以将这些 ID 设置为筛选条件:

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

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

huangapple
  • 本文由 发表于 2023年7月27日 19:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定