首页 > 解决方案 > 在 Shopware 6 中将自定义实体添加到电子邮件模板

问题描述

我正在尝试向我的电子邮件模板添加一个新实体,所以我有:1)我用我的自定义实体创建了一个新插件

        $connection->executeQuery('
            CREATE TABLE IF NOT EXISTS `my_entity` (
              `id` BINARY(16) NOT NULL,
              `message` VARCHAR(255) NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
        ');

2)我在此列中添加了第一行:

id      | name
bin... | test

3) 我在 db: mail_template_typecolumn中编辑这条记录available_entities {"order":"order","salesChannel":"sales_channel","myEntity":"my_entity"}

现在当我编辑邮件时,twig 告诉我有这样一个变量可以使用,但是当我使用它时,里面什么都没有{{ myEntity.name }}

我可以有一个明确的解释,我该怎么做?

标签: phpentityshopware

解决方案


If you dispatch your own Event to send the mail, you have to implement the getAvailableData function and add your Entity to the Collection

public static function getAvailableData(): EventDataCollection
{
    return (new EventDataCollection())
        ->add('myEntity', new EntityType(MyEntityDefinition::class));
}

Additionally you have to create a getter function to provide the data to the mail template.

public function getMyEntity(): MyEntityEntity
{
    return $this->myEntity;
}

Otherwise if you want to add an entity to an already existing event you can add an association to the entity which is used in the event.


推荐阅读