繁体   English   中英

Shopware6 中具有自定义事件的自定义实体

[英]Custom Entity with Custom Event in Shopware6

几天来,我一直在尝试在 Shopware6 中创建带有自定义实体和事件的插件,因为我想将自己的实体添加到 email 模板中。

我想做什么?

在每个 email 模板中,我想使用我的实体: {{ customEntity.technicalName[0] }} (该字段是从数据库加载的)。

所以我从 Shopware github: swag swag-docs-custom-entity选择了这个存储库,并尝试创建一个自定义事件。

<?php
namespace Swag\CustomEntity\Event;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Event\EventData\EntityType;
use Shopware\Core\Framework\Event\EventData\EventDataCollection;
use Shopware\Core\Framework\Event\EventData\MailRecipientStruct;
use Shopware\Core\Framework\Event\MailActionInterface;
use Swag\CustomEntity\Custom\CustomEntity;
use Swag\CustomEntity\Custom\CustomEntityDefinition;

class CustomEvent extends Event implements MailActionInterface
{
    public const EVENT_NAME = 'custom.entity';

    /** @var CustomEntity */
    private $customEntity;

    /** @var Context */
    private $context;

    public function __construct(CustomEntity $customEntity, Context $context)
    {
        $this->customEntity = $customEntity;
        $this->context = $context;
    }
    public static function getAvailableData(): EventDataCollection
    {
        return (new EventDataCollection())
            ->add('customEntity', new EntityType(CustomEntityDefinition::class));
    }
    public function getCustomEntity(): CustomEntity
    {
        return $this->customEntity;
    }
    public function getName(): string
    {
        return self::EVENT_NAME;
    }
    public function getMailStruct(): MailRecipientStruct
    {
        $technicalName = $this->customEntity->getTechnicalName();
        return new MailRecipientStruct([
            $technicalName
        ]);
    }
    public function getSalesChannelId(): ?string
    {
        return null;
    }
    public function getContext(): Context
    {
        return $this->context;
    }
}

和我的服务。xml:

<services>
    <service id="Swag\CustomEntity\Custom\CustomEntityDefinition">
        <tag name="shopware.entity.definition" entity="custom_entity" />
    </service>

    <service id="Swag\CustomEntity\Event\CustomEvent" public="true">
        <argument type="collection">
            <argument key="custom.entity" type="collection">
                <argument key="customEntity" type="collection">
                    <argument key="type">entity</argument>
                    <argument key="entityClass">Swag\CustomEntity\Custom\CustomEntityDefinition</argument>
                </argument>
            </argument>
        </argument>
        <argument type="service" id="assets.context"/>
    </service>
</services>

我在联系表格中编辑了 available_entites:

{"salesChannel":"sales_channel","customEntity":"custom_entity"}

现在当我尝试发送联系表时,我的 rest 是302 FOUND并且邮件没有发送。

你的模板是什么样的? 如果getSalesChannelId function 返回null您无权访问模板中的SalesChannel 因此,您可用的实体将是:

{"customEntity":"custom_entity"}

由于它用于联系表单,因此您应该返回销售渠道 ID。


如果尚未完成,您需要在event_action表中添加一个条目,以便在发送 BusinessEvent 时发送邮件。

如果邮件仍未发送,请查看var/log/dev.log可能会显示错误。

HoelShare,我看到了您的存储库名称 ExampleEventEntity 并且它有效。 但我想将我的实体添加到现有的邮件模板中。 它可以改变吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM