【问题标题】:Shopware 6 Products with Custom fields typeShopware 6 具有自定义字段类型的产品
【发布时间】:2020-11-21 09:38:53
【问题描述】:

在后端我定义了几个自定义字段,它们都是选择类型,这些都与产品相关联。现在我想在详细页面中显示 product.customFields 的选定选项的值。

我可以转储 product.customFields 并获得这样的数组:

    product.customFields array [
       technical_name_select_field_1 => option_xxx
       technical_name_select_field_2 => option_123
 ]

现在我如何获取存储在 custom_field 中的值作为 json 对象,用 name = Technical_name_select_field_1 和 config = {{"type": "select", "label": {"de-DE": “Wert für option xxx”、“en-US”:“option xxx 的值”、“nl-NL”:“Waarde optie xxx”)、“value”:“option_xxx”} 等。

当我在模板中写 {{ attribute(product.translated.customFields,'technical_name_select_field_1')|sw_sanitize }} 时,我会打印 option_xxx。但我想查看标签值“选项 xxx 的值”等。

【问题讨论】:

    标签: custom-fields shopware6


    【解决方案1】:

    AFAIK,自定义字段定义和任何实体中的自定义字段值之间没有直接联系。

    我可以建议您订阅 sales_channel.product.loaded 并使用您需要的信息扩展产品数据。

    在订阅者中,您可以直接调用custom_field存储库并通过自定义字段的技术名称搜索定义,您在产品数据中作为键。

    【讨论】:

    • 你好我也有这个问题,你能解释一下如何做到这一点吗?我是新来的商店用品。谢谢
    • 嗨@Sanne,您到底想知道什么,如何订阅事件或从 custom_field 存储库中检索数据?
    • 嗨@Valerrii,是的,我不知道从哪里开始,所以我猜都是?我目前在我的树枝文件中有:{{ product.translated.customFields.migration_attribute_4_streek_154 | trans }} 从而产生 option_234
    • 好的,您可以查看 \Shopware\Core\Content\Product\SalesChannel\SalesChannelProductSubscriber 以了解如何在此创建订阅者。在构造函数中,您应该将custom_field.repository 传递给您自己的订阅者。在loaded 方法中,您可以收集自定义字段的技术名称并使用存储库查找其定义。之后,您可以将任何信息添加到您的产品数据(实体)中,我更喜欢将其添加到扩展中,因为 Shopware Entity 是 Shopware\Core\Framework\Struct 的一个实例。
    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2014-08-20
    • 2022-08-16
    • 1970-01-01
    • 2023-01-20
    • 2022-11-05
    • 2021-06-24
    相关资源
    最近更新 更多