0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EC CUBE お問い合わせフォームを複製し、再入荷通知機能を追加する

Last updated at Posted at 2019-07-09

想定URL

www.hogehoge.com/Restock?item_code=00000&item_name=hogehoge

パラメーターで品番、品名を取得

EccubeEvents.php

イベントの追加

// 再入荷通知
    /**
     * RestockController
     */
    // index
    const FRONT_RESTOCK_INDEX_INITIALIZE = 'front.restock.index.initialize';
    const FRONT_RESTOCK_INDEX_COMPLETE = 'front.restock.index.complete';
    /**
     * MailService
     */
    const MAIL_RESTOCK = 'mail.restock'; // 再入荷通知

FrontControllerProvider.php

//contact から2行を複製
URL ルーティング設定
関連箇所を編集

//  restock   再入荷通知
$c->match('/restock', '\Eccube\Controller\RestockController::index')->bind('restock');
$c->match('/restock/complete', '\Eccube\Controller\RestockController::complete')->bind('restock_complete');

RestockController.php

ContactController.phpを複製し作成
関連箇所を編集

品番と商品名をGETパラメーターより取得

$item_code = $request->query->get('item_code');
$item_name = $request->query->get('item_name');
		
        if ($app->isGranted('ROLE_USER')) {
            $user = $app['user'];
            $builder->setData(
                array(
                    'name01' => $user->getName01(),
                    'name02' => $user->getName02(),
                    'company_name' => $user->getCompanyName(),
                    'email' => $user->getEmail(),
					// フォームタイプにセット
					'item_code' => $item_code,
					'item_name' => $item_name,
                )
            );
        }

パラメーターなしのURLへのアクセスがあった場合にエラーを返す
完了画面のレンダリング前に挿入

		// item_code,nameがセットされていなければ
		//パラメーターが空の場合エラー処理
		if ($item_code == NULL or $item_name == NULL ){
			return( '不正な処理です' );
        }

取得した品番、品名をレンダリングで受け渡し

        return $app->render('Restock/index.twig', array(
            'form' => $form->createView(),
			// パラメーター 直でレンダリングする場合
			'item_code' => $item_code,
			'item_name' => $item_name,
        ));

EccubeServiceProvider.php

フォームタイプを追加

// front
・
・
・
$types[] = new \Eccube\Form\Type\Front\RestockType($app['config']); // 再入荷通知

RestockType.php

ContactType.phpを複製し作成
関連箇所を編集
RestockController.phpで取得した情報のフォームタイプを指定

            ->add('name01', 'hidden', array(
                'required' => true,
            ))
            ->add('name02', 'hidden', array(
                'required' => true,
            ))
            ->add('company_name', 'hidden', array(
                'required' => true,
				'label' => '会社名',
            ))
            ->add('item_code', 'hidden', array(
                'required' => false,
				'label' => '品番',
            ))
            ->add('item_name', 'hidden', array(
                'required' => false,
				'label' => '商品名',
            ))
            ->add('email', 'email', array(
                'required' => true,
                'constraints' => array(
                    new Assert\NotBlank(),
                    new Assert\Email(array('strict' => true)),
                ),
            ));

Restock/hogehoge.twig

テンプレート一式

Restock/index.twig - フォーム画面

                       <form name="form1" id="form1" method="post" action="{{ url('restock') }}">
                             {{ form_widget(form._token) }}
                            <div id="top_box__body_inner" class="dl_table">
                            
							{{ form_row(form.name01) }}
							{{ form_row(form.name02) }}
							{{ form_row(form.company_name) }}
											
                                <dl id="top_box__email">
                                    <dt>{{ form_label(form.item_code) }}</dt>
                                    <dd>
                                        <div class="form-group">
											{{ item_code }}		
											
											{{ form_widget(form.item_code) }} 
                                            {{ form_errors(form.item_code) }}
                                        </div>
                                    </dd>
                                </dl>
                                <dl id="top_box__email">
                                    <dt>{{ form_label(form.item_name) }}</dt>
                                    <dd>
                                        <div class="form-group">
                                        	{{ item_name }}		
											
                                            {{ form_widget(form.item_name) }} 
                                            {{ form_errors(form.item_name) }}
                                        </div>
                                    </dd>
                                </dl>
								
                                <dl id="top_box__email">
                                    <dt>{{ form_label(form.email) }}</dt>
                                    <dd>
                                        <div class="form-group">
                                            {{ form_widget(form.email) }}
                                            {{ form_errors(form.email) }}
                                        </div>
                                    </dd>
                                </dl>
                            </div>

Restock/complete.twig - 完了画面

任意で編集

今回は確認画面は使用しない(Restock/confirm.twig)

Contact/index.twig , confirm.twig , complete.twig を複製し作成
関連箇所を編集

MailService.php

メール送信機能
sendContactMailを複製

		// 再入荷通知
	
		/**
		 * Send  restock mail.
		 *
		 * @param $formData 再入荷通知内容
		 */
		public function sendRestockMail($formData)
		{
			log_info('再入荷通知受付メール送信開始');

			$body = $this->app->renderView('Mail/restock_mail.twig', array(
				'data' => $formData,
				'BaseInfo' => $this->BaseInfo,
			));

			// 問い合わせ者にメール送信
			$message = \Swift_Message::newInstance()
				->setSubject('[' . $this->BaseInfo->getShopName() . '] 再入荷お知らせメールのお申し込みを受け付けました。')
				->setFrom(array($this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()))
				->setTo(array($formData['email']))
				->setBcc($this->BaseInfo->getEmail01())
				->setReplyTo($this->BaseInfo->getEmail01())
				->setReturnPath($this->BaseInfo->getEmail01())
				->setBody($body);

			$event = new EventArgs(
				array(
					'message' => $message,
					'formData' => $formData,
					'BaseInfo' => $this->BaseInfo,
				),
				null
			);
			$this->app['eccube.event.dispatcher']->dispatch(EccubeEvents::MAIL_RESTOCK, $event);

			$count = $this->app->mail($message);

			log_info('再入荷通知受付メール送信完了', array('count' => $count));

			return $count;
		}

		/**
		 * Alias of sendRestockMail().
		 *
		 * @param $formData 再入荷通知内容
		 * @see sendRestockMail()
		 * @deprecated since 3.0.0, to be removed in 3.1
		 * @link https://github.com/EC-CUBE/ec-cube/issues/1315
		 */
		public function sendrRestockMail($formData)
		{
			$this->sendRestockMail($formData);
		}

Mail/restock_mail.twig

確認メールの自動配信

※本メールは自動配信メールです。

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
{{ BaseInfo.shop_name }} 再入荷お知らせメールお申し込み受付完了
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

{{ data.company_name }}
{{ data.name01 }} {{ data.name02 }} 様

いつも{{ BaseInfo.shop_name }}をご利用いただき、ありがとうございます。

以下の内容で再入荷お知らせメールのお申し込みを受け付けました。

■商品情報
品番:{{ data.item_code }}
商品名:{{ data.item_name }}

■ご通知先メールアドレス
{{ data.email }}

入荷次第ご連絡いたしますので、お待ちください。
0
1
2

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?