# Custom Fields in Mailtemplate

**URL:** <https://forum.shopware.com/t/custom-fields-in-mailtemplate/93993>\
**Category:** Programmierung\
**Created:** [20. April 2022 um 09:36 UTC](https://forum.shopware.com/t/custom-fields-in-mailtemplate/93993 "2022-04-20T09:36:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yicdeniz](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/yicdeniz/32/7847_2.png) [@yicdeniz](https://forum.shopware.com/u/yicdeniz)\
**Post date:** [20. April 2022 um 09:36 UTC](https://forum.shopware.com/t/custom-fields-in-mailtemplate/93993/1 "2022-04-20T09:36:32Z")

</div>

Hallo,

ich möchte in den E-Mail-Vorlagen meine CustomFields verwenden. Hierbei handelt es sich um Felder, die im Backend verwendet werden. Diese sollen dann beim Versand einer Mail genutzt werden.

Nach folgender Anleitung habe ich hierfür einen Decorator erstellt:

> **[Add data to mails](https://developer.shopware.com/docs/guides/plugins/plugins/content/mail/add-data-to-mails)**

Wenn ich dann {{ myCustomData }} in meinem E-Mail-Template verwende, wird auch „Example data“ ausgegeben.

Nun möchte ich das ganze mit Custom Fields verknüpfen. Dafür habe ich die CustomFields direkt über das Plugin erstellt

```auto
public function install(InstallContext $installContext): void
    {

        $customFieldSetRepository = $this->container->get('custom_field_set.repository');
        $customFieldSetRepository->create([
            [
                'name' => 'custom_example',

                'config' => [
                    'label' => [
                        'de-DE' => 'Example Label',
                        'en-GB' => 'Example Label'
                    ]
                ],

                'relations' => [[
                    'entityName' => 'order'
                ]],

                'customFields' => [
                    [
                        'name' => 'custom_example_one',
                        'type' => CustomFieldTypes::HTML,
                        'config' => [
                            'label' => [
                                'de-DE' => 'Example 1',
                                'en-GB' => 'Example 1',
                            ],
                            'componentName' => "sw-text-editor",
                            'customFieldType' => "textEditor"
                        ]
                    ]
                ]
            ],
        ], $installContext->getContext());
    }

```

Die Datei AddDataToMails, die nach der Anleitung erstellt habe, sieht wie folgt aus:

```auto
<?php declare(strict_types=1);

namespace Blauband\EmailBase\Service;

use Shopware\Core\Content\Mail\Service\AbstractMailService;
use Shopware\Core\Framework\Context;
use Symfony\Component\Mime\Email;

class AddDataToMails extends AbstractMailService
{
    /**
     * @var AbstractMailService
     */
    private AbstractMailService $mailService;

    public function __construct(AbstractMailService $mailService)
    {
        $this->mailService = $mailService;
    }

    public function getDecorated(): AbstractMailService
    {
        return $this->mailService;
    }

    public function send(array $data, Context $context, array $templateData = []): ?Email
    {
        $templateData['myCustomData'] = 'Example data';

        return $this->mailService->send($data, $context, $templateData, );
    }
}

```

Ich weiß nicht, wie ich in die Variable $templateData[‚myCustomData‘] die Daten aus einer Customfield laden kann…

Hat hierzu jemand eine Idee?

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [20. April 2022 um 11:32 UTC](https://forum.shopware.com/t/custom-fields-in-mailtemplate/93993/2 "2022-04-20T11:32:13Z")

</div>

Da musst du wohl ein Repository in dein \_\_construct mit laden und dann über das Repository die Daten laden und der Variable zuweisen.

Die Frage ist, wieso das überhaupt notwendig ist. Wenn du das dem Entity Order zuweist, dann würde ich aus dem Bauch heraus sagen, dass du über Order.customFields darauf zugreifen können solltest. Ist wie gesagt aber nur ein Bauchgefühl. Habe den konkreten Fall nicht als Code vor Augen.

---

<div class="post-metadata">

**Author:** ![yicdeniz](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/yicdeniz/32/7847_2.png) [@yicdeniz](https://forum.shopware.com/u/yicdeniz)\
**Post date:** [20. April 2022 um 12:28 UTC](https://forum.shopware.com/t/custom-fields-in-mailtemplate/93993/3 "2022-04-20T12:28:23Z")

</div>

Was meinst du genau mit Entity Order zuweisen?

Meinst du damit die Anlage einer Zusatzfeldes mit einer Zuweisung zu Bestellungen (Order)?

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [20. April 2022 um 12:36 UTC](https://forum.shopware.com/t/custom-fields-in-mailtemplate/93993/4 "2022-04-20T12:36:57Z")

</div>

> [@yicdeniz](#):
>
> ```auto
> 'relations' => [[
> 'entityName' => 'order'
> ]]
> 
> ```

Ja, genau, das meine ich damit.

---

<div class="post-metadata">

**Author:** ![yicdeniz](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/yicdeniz/32/7847_2.png) [@yicdeniz](https://forum.shopware.com/u/yicdeniz)\
**Post date:** [21. April 2022 um 06:53 UTC](https://forum.shopware.com/t/custom-fields-in-mailtemplate/93993/5 "2022-04-21T06:53:54Z")

</div>

Folgendes habe ich verwendet - klappt:

{{ order.customFields.custom\_order\_custom\_example\_one }}
