簡體   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