我try 在几天内获取一个Collection类的元素的"名称"array.我try 了在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('Tags');
        $criteria1->setIds($currentPropertyOption);

        $tags = new TagCollection();
        $options = $this->optionRepository->search($criteria1, $event->getContext())->getEntities();

        foreach ($options as $option) {
            $extension = $option->getExtension('myTags');
            
            if (!$extension instanceof TagCollection) {
                continue;
            }
            
            $tags->merge($extension);
        }

        $tagnames = $tags->getElements('myTags');
        // doesn't work $tagnames = $options->getExtension('myTags')->get('names');
        // doesn't work $tagnames = $options->getExtension('myTags')->getValues();
        // etc..

        // test $event->getContext()->addExtension('myTags1', $tags);
        // test $event->getContext()->addExtension('myTagsNames1', new ArrayEntity($tagnames));

下面是myTags1 EXTEXT转储:

Shopware\Core\System\Tag\TagCollection {#8104 ▼
  #elements: array:10 [▼
    "49aa54d94e394bc2affb5b702d402e42" => Shopware\Core\System\Tag\TagEntity {#12939 ▶}
    "551fcda8a25f47c990d7a795ef251508" => Shopware\Core\System\Tag\TagEntity {#12943 ▶}
    "8c526404df194a88bf72073fea2fdabd" => Shopware\Core\System\Tag\TagEntity {#12947 ▼
      #name: "Name1"

有人知道如何获得一组名字吗?

推荐答案

foreach ($options as $option) {
    $extension = $option->getExtension('myTags');
    
    if (!$extension instanceof TagCollection) {
        continue;
    }
    
    $tags->merge($extension);
}

$names = $tags->map(fn (TagEntity $tag) => $tag->getName());

Php相关问答推荐

如何使用动态键来获取Laravel Livewire表格中的错误?

用户信息更新期间Laravel邮箱验证问题

Laravel 9如何过滤$的项目与结果?

WooCommerce中仅在单一产品和产品档案页面中将小数设置为大写

在PHP中循环访问多维数组,并按组显示内容

如何使用php一次更新两个数据库中的MySQL表

根据WooCommerce购物车页面中的自定义 Select 添加费用

免费送货柜台,考虑到折扣码

如何在PHP中组合日期范围

在 WooCommerce 更新结帐事件上动态更新自定义内容

PHP访问子数组列表出错结果和长度

图片上传成功

使用 phpseclib 验证 RSA-PSS

在 vue 自定义渲染器中访问变量

在 WooCommerce wc_get_products 函数中处理自定义分类法

Laravel Eloquent(where 子句)

如何将文件上传添加到 WooCommerce 结帐?

WooCommerce – 在新订单邮箱通知中显示产品分类计数

Laravel 8:会话不适用

为什么 8.0 之前的 PHP 版本拒绝命名空间Random\Namespace作为无效语法?