I'm working on a Shopware 6 project where I need to implement custom fields for customers. The task involves creating a custom field set named "Swag Customer" with two additional fields: "Purchase on invoice" and "Purchase on direct debit".
The requirements are:
I am little confused should I create these fields in the config.xml
or create them in a PHP
file?
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
as shown on here
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);
The second option using customFieldSetRepository has to be adopted, since its in the official documentation of Shopware. "Additionally just as a note, this should be included in the new plugin that you will create."