Shopware 6 插件:如何为客户实现自定义字段?

问题描述 投票:0回答:1

我正在开发一个 Shopware 6 项目,我需要为客户实现自定义字段。该任务涉及创建一个名为“Swag Customer”的自定义字段集,其中包含两个附加字段:“发票上的购买”和“直接借记上的购买”。

要求是:

我有点困惑应该在

config.xml
中创建这些字段还是在
PHP
文件中创建它们?

config.xml

<custom-fields>
    <custom-field-set name="Swag Kunden" entity="customer">
        <custom-field name="custom_swag_customer_purchaseoninvoice" type="checkbox">
            <label lang="en">Purchase on invoice</label>
        </custom-field>
        <custom-field name="custom_swag_customer_purchaseondirectdebit" type="checkbox">
            <label lang="en">Purchase on direct debit</label>
        </custom-field>
    </custom-field-set>
</custom-fields>

CustomFieldClass.php
此处

所示
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Defaults;

// Create custom field set
$this->customFieldSetRepository->create([
    [
        'name' => 'custom_swag_customer', // Technical name of the custom field set
        'config' => [
            'label' => [
                'en-GB' => 'swag Customer', // English custom field set label
                Defaults::SYSTEM_LANGUAGE => 'swag Customer' // Fallback label
            ]
        ],
        'customFields' => [
            [
                'name' => 'custom_swag_customer_purchaseoninvoice', // Technical name of the custom field
                'type' => CustomFieldTypes::BOOL, // Data type of the custom field (checkbox)
                'config' => [
                    'label' => [
                        'en-GB' => 'Purchase on invoice', // English custom field label
                        Defaults::SYSTEM_LANGUAGE => 'Purchase on invoice' // Fallback label
                    ],
                    'customFieldPosition' => 1 // Position of the custom field
                ]
            ],
            [
                'name' => 'custom_swag_customer_purchaseondirectdebit', // Technical name of the custom field
                'type' => CustomFieldTypes::BOOL, // Data type of the custom field (checkbox)
                'config' => [
                    'label' => [
                        'en-GB' => 'Purchase on direct debit', // English custom field label
                        Defaults::SYSTEM_LANGUAGE => 'Purchase on direct debit' // Fallback label
                    ],
                    'customFieldPosition' => 2 // Position of the custom field
                ]
            ]
        ]
    ]
], $context);
symfony symfony4 shopware shopware6
1个回答
0
投票

必须采用使用 customFieldSetRepository 的第二个选项,因为它在 Shopware 的官方文档中。 “此外,请注意,这应该包含在您将创建的新插件中。”

© www.soinside.com 2019 - 2024. All rights reserved.