ECCUBE4

【EC-CUBE4】 注文確定メール送信時にBccを追加する

こんにちは。

今回は、EC-CUBE4で注文確定時に、管理側より登録したBcc専用のメールアドレスに送信する方法を紹介します。

この記事で学べる事

・BaseInfoエンティティの拡張方法
・管理ページにフォームを追加する
・メール送信時のイベント追加処理

管理側のメールアドレス登録処理の作成

1. BaseInfoエンティティを拡張

Bcc送信用のメールアドレスを登録するカラムをdtb_base_infoに追加するためのファイルを作成します。

1-1. BaseInfoTrait.phpの作成

$ vi app/Customize/Entity/BaseInfoTrait.php

1-2. bcc送信用のメールアドレスカラムを追加します。

<?php

namespace Customize\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eccube\Annotation\EntityExtension;

/**
 * @EntityExtension("Eccube\Entity\BaseInfo")
 */
trait BaseInfoTrait
{
    /**
     * @var string|null
     *
     * @ORM\Column(name="bcc_mailaddress", type="string", length=255, nullable=true)
     */
    private $bcc_mailaddress;

    /**
     * Set bcc_mailaddress.
     *
     * @param string|null $bcc_mailaddress
     *
     * @return BaseInfo
     */
    public function setBccMailaddress($bcc_mailaddress = null)
    {
        $this->bcc_mailaddress = $bcc_mailaddress;

        return $this;
    }

    /**
     * Get bcc_mailaddress.
     *
     * @return string|null
     */
    public function getBccMailaddress()
    {
        return $this->bcc_mailaddress;
    }
}

3. データベースに反映させます。

// proxyの生成
$ php bin/console e:g:p

// データベースに反映 
$ php bin/console d:s:u --dump-sql --force

2. 基本設定ページに登録フォームを追加

2-1. BaseInfoのフォームタイプを拡張するファイルを作成します。

// ディレクトリを作成
$ mkidr mkdir app/Customize/Form
$ mkdir app/Customize/Form/Extension

// フォームの拡張ファイルを作成
$ vi app/Customize/Form/Extension/BaseInfoExtension.php

2-2. フォームタイプを編集します。

<?php

namespace Customize\Form\Extension;

use Eccube\Common\EccubeConfig;
use Eccube\Form\Type\Admin\ShopMasterType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Eccube\Form\Validator\Email;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\EmailType;


class BaseInfoExtension extends AbstractTypeExtension
{
    /**
     * @var EccubeConfig
     */
    protected $eccubeConfig;

    /**
     * ShopMasterType constructor.
     *
     * @param EccubeConfig $eccubeConfig
     */
    public function __construct(EccubeConfig $eccubeConfig)
    {
        $this->eccubeConfig = $eccubeConfig;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('bcc_mailaddress', EmailType::class, [
            'required' => false,
            'constraints' => [
                new Assert\NotBlank(),
                new Email(['strict' => $this->eccubeConfig['eccube_rfc_email_check']]),
            ],
        ]);
    }

    /**
     * 
     */
    public function getExtendedType()
    {
        return ShopMasterType::class;
    }

}

2-3. Twigファイルに反映する。

「src/Eccube/Resource/template/admin/Setting/Shop/shop_master.twig」の155行あたりに追記します。

                            <div class="row mb-3">
                                <div class="col-3">
                                    <div class="d-inline-block">
                                        Bcc送信メールアドレス
                                    </div>
                                </div>
                                <div class="col mb-2">
                                    {{ form_widget(form.bcc_mailaddress) }}
                                    {{ form_errors(form.bcc_mailaddress) }}
                                </div>
                            </div>

「設定」:「店舗設定」:「基本設定」にフォームが追加されていることを確認します。

注文メール送信のイベントを追加

メール送信のイベント処理に管理画面で登録したBccのメールアドレスを追加して、メールを送信する処理を差し込みます。

1. 注文メール送信時のイベントを読み込むファイルを作成

// イベントを処理するファイルを保管するディレクトリの作成
$ mkdir app/Customize/Event

// ファイル作成
$  touch app/Customize/Event/OrderMailEvent.php

2. 注文メール送信時のイベントを作成

<?php

namespace Customize\Event;

use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Repository\BaseInfoRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderMailEvent implements EventSubscriberInterface
{
    /**
     * @var BaseInfoRepository
     */
    private $baseInfo;

    /**
     * @param BaseInfoRepository $baseInfo
     */
    public function __construct
    (
        BaseInfoRepository $baseInfo
    )
    {
        $this->baseInfo = $baseInfo->get();
    }

    /**
     * @param EventArgs $event
     */
    public function mailChange(EventArgs $event)
    {
        if ($this->baseInfo->getBccMailAddress()) {
            $auguments = $event->getArguments();
            $message = $auguments['message'];
            $message->addBcc($this->baseInfo->getBccMailAddress());
            $auguments['message'] = $message;
            $event->setArguments($auguments);
        }
        return;

    }

    public static function getSubscribedEvents()
    {
        return [
            EccubeEvents::MAIL_ORDER => 'mailChange',
        ];
    }
}

最後に、注文動作を行いBccに基本設定で登録したメールアドレスが追加されているか確認しましょう。