Search code examples
phpcustom-fieldsshopware

How to add a custom field to orders in Shopware 6?


By default you can add custom fields to several entities, however I don’t see the order entity in the list of available entities.

Is it possible to add such a field for order so user can fill it in the checkout process, right before sending the order?

And is it possible to add a field for the order and for each order item individually?


Solution

  • Here is one example on how to add to add custom fields to order entity:

    $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);