0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【EC-CUBE4.3】プラグインを開発してみる その5

Posted at

本記事で分かること

  • プラグインでのイベントリスナーの使い方

手順

1. イベントリスナーでorder_noをフォームにセット

  • 現時点だと、受注問合せボタンを押しても、問い合わせフォームの受注番号が空のまま。
namespace Plugin\OrderInquiry\EventListener;

use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ContactFormInitializeListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            EccubeEvents::FRONT_CONTACT_INDEX_INITIALIZE => 'addOrderNoField',
        ];
    }

    public function addOrderNoField(EventArgs $event)
    {
        $builder = $event->getArgument('builder');
        $request = $event->getRequest();

        $orderNo = $request->query->get('order_no');

        if (!$orderNo) {
            return;
        }

        $data = $builder->getData() ?? [];
        $data['order_no'] = $orderNo;
        $builder->setData($data);
    }
}

イベントリスナーは、プラグインでも通常の使い方と同じ。

2. 実際に確認してみる

image.png

  • 注文番号がセットされるようになった
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?