You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Shopware 6中获取Collection元素的名称数组(PHP)

解决Shopware 6中从TagCollection获取名称数组的问题

首先,你的代码存在几处需要修正的细节,之后就能正确提取标签名称数组:

1. 修正变量名错误

创建$criteria1时使用了未定义的变量$currentPropertyOption,应改为复数形式的$currentPropertyOptions:

$criteria1->setIds($currentPropertyOptions);

2. 修正关联扩展名称

你添加的关联是'Tags',因此OptionEntity上的扩展名称应为'tags'(小写开头),而非'myTags':

$extension = $option->getExtension('tags'); // 替换'myTags'为'tags'

3. 提取Tag名称数组

当$tags集合正确填充TagEntity对象后,遍历集合并收集每个标签的名称即可:

$tagNames = [];
foreach ($tags as $tag) {
    if ($tag instanceof \Shopware\Core\System\Tag\TagEntity) {
        $tagNames[] = $tag->getName();
    }
}

或者使用更简洁的array_map写法:

$tagNames = array_map(function (\Shopware\Core\System\Tag\TagEntity $tag) {
    return $tag->getName();
}, $tags->getElements());

此时$tagNames即为你需要的标签名称数组。

内容的提问来源于stack exchange,提问作者Stacker1427

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 02:00:31