【问题标题】:How to add a custom field to orders in Shopware 6?如何在 Shopware 6 中为订单添加自定义字段?
【发布时间】:2020-10-30 18:56:18
【问题描述】:

默认情况下,您可以向多个实体添加自定义字段,但是我在可用实体列表中看不到订单实体。

是否可以为订单添加这样一个字段,以便用户可以在发送订单之前在结帐过程中填写它?

是否可以为订单和每个订单项目单独添加一个字段?

【问题讨论】:

  • 目前无法通过管理设置模块向订单添加自定义字段。但是您仍然可以通过插件添加自定义字段。
  • 是否可以在结帐页面填写自定义字段的值?在 sw5 中,我通过将响应转发给我的控制器然后返回来做到这一点。
  • 您可能想要使用CartConvertedEvent 事件。这包含原始购物车和订单数据作为将写入数据库的数组。将自定义字段的值设置为订单数组
  • @MichaelT 完整活动的名称是什么?是 OrderEvents: CartConvertedEvent 吗?
  • 不,它是Shopware\Core\Checkout\Cart\Order\CartConvertedEvent 使用::class 常量在订阅者中引用此事件

标签: php custom-fields shopware


【解决方案1】:

这是一个关于如何添加自定义字段到订单实体的示例:

$customFieldSetRepository = $this->container->get('custom_field_set.repository');
    
            $customFieldSetRepository->upsert([
                [
                    'name' => self::FIELD_NAME,
                    // 'global' => true,
                    'config' => [
                        'label' => [
                            'de-DE' => 'Name',
                            'en-GB' => 'Name'
                        ]
                    ],
                    'customFields' => [
                        [
                            'name' => 'name',
                            'type' => CustomFieldTypes::DATETIME,
                            'config' => [
                                'type' => 'date',
                                'dateType' => 'date',
                                'label' => [
                                    'de-DE' => 'Date',
                                    'en-GB' => 'Date'
                                ]
                            ]
                        ],
                        [
                            'name' => 'name',
                            'label' => "Time",
                            'type' => CustomFieldTypes::TEXT,
                            'config' => [
                                'label' => [
                                    'de-DE' => 'name',
                                    'en-GB' => 'name'
                                ]
                            ]
                        ],
                        [
                            'name' => 'name',
                            'label' => "name",
                            'type' => CustomFieldTypes::INT,
                            'config' => [
                                'label' => [
                                    'de-DE' => 'name',
                                    'en-GB' => 'name'
                                ]
                            ]
                        ]
                    ],
                    'relations' => [[
                        'entityName' => 'order'
                    ]],
                ]
            ], $context);

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 2021-06-01
    • 1970-01-01
    • 2018-12-31
    • 2020-12-30
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多