app/Plugin/tbsTrialProduct42/tbsTrialProductEvent.php line 118

Open in your IDE?
  1. <?php
  2. namespace Plugin\tbsTrialProduct42;
  3. use Eccube\Entity\Customer;
  4. use Eccube\Entity\ProductClass;
  5. use Eccube\Event\EventArgs;
  6. use Eccube\Event\TemplateEvent;
  7. use Plugin\tbsTrialProduct42\Repository\tbsTrialProductRepository;
  8. use Psr\Container\ContainerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class tbsTrialProductEvent implements EventSubscriberInterface
  11. {
  12.     /** @var tbsTrialProductRepository */
  13.     private $tbsTrialProductRepository;
  14.     /** @var ContainerInterface */
  15.     private $container;
  16.     /** @var Customer */
  17.     private $user;
  18.     /**
  19.      * tbsTrialProductEvent constructor.
  20.      *
  21.      * @param tbsTrialProductRepository $tbsTrialProductRepository
  22.      * @param ContainerInterface $container
  23.      */
  24.     public function __construct(tbsTrialProductRepository $tbsTrialProductRepositoryContainerInterface $container)
  25.     {
  26.         $this->tbsTrialProductRepository $tbsTrialProductRepository;
  27.         $this->container $container;
  28.         $token $this->container->get('security.token_storage')->getToken();
  29.         if ($token) {
  30.             $this->user $token->getUser();
  31.         }
  32.     }
  33.     /**
  34.      * 商品一覧でお試し商品を非表示にする.
  35.      *
  36.      * @param EventArgs $event
  37.      */
  38.     public function onRenderProductIndexSearch(EventArgs $event)
  39.     {
  40.         $qb $event->getArgument('qb');
  41.         // DBから設定情報を取得
  42.         $template $this->tbsTrialProductRepository->findOne();
  43.         // ログイン済みかつ購入履歴がある場合は対象商品を非表示
  44.         if ($template && $template['first_only'] == && $this->user && $this->user != 'anon.' && $this->user->getBuyTimes() > 0) {
  45.             // 商品規格がjoinされていない場合はここでjoin
  46.             $isJoin false;
  47.             $joins $qb->getDQLPart('join');
  48.             if ($joins && !empty($joins['p'])) {
  49.                 foreach ($joins['p'] as $join) {
  50.                     if ($join->getAlias() == 'pc') {
  51.                         $isJoin true;
  52.                         break;
  53.                     }
  54.                 }
  55.             }
  56.             if (!$isJoin) {
  57.                 $qb->innerJoin('p.ProductClasses''pc');
  58.             }
  59.             // お試し商品以外を表示
  60.             $qb
  61.                 ->leftJoin('pc.SaleType''pt')
  62.                 ->andWhere('pt.id!=:id')
  63.                 ->setParameter('id'$template['ProductSaleType']->getId());
  64.         }
  65.     }
  66.     /**
  67.      * 商品一覧のカートボタンを変更する.
  68.      *
  69.      * @param TemplateEvent $event
  70.      */
  71.     public function onRenderProductList(TemplateEvent $event)
  72.     {
  73.         $parameters $event->getParameters();
  74.         $pagination $parameters['pagination'];
  75.         if (empty($pagination)) {
  76.             return;
  77.         }
  78.         // DBから設定情報を取得
  79.         $template $this->tbsTrialProductRepository->findOne();
  80.         // 未ログインの場合と購入履歴がある場合は対象販売種別のカートボタンを非アクティブ化
  81.         if ($template && $template['first_only'] == 1) {
  82.             $cartButtonName null;
  83.             // 未ログインの場合
  84.             if (!$this->user || $this->user == 'anon.') {
  85.                 $cartButtonName trans('tbs_trial_product.front.cart.button.login');
  86.             // 購入履歴がある場合
  87.             } elseif ($this->user->getBuyTimes() > 0) {
  88.                 $cartButtonName trans('tbs_trial_product.front.cart.button.first_only');
  89.             }
  90.             if ($cartButtonName) {
  91.                 $parameters $event->getParameters();
  92.                 $parameters['ProductSaleTypeId'] = $template['ProductSaleType']->getId();
  93.                 $parameters['cartButtonName'] = $cartButtonName;
  94.                 $event->setParameters($parameters);
  95.                 $event->addSnippet('@tbsTrialProduct42/default/product_trial_list.twig');
  96.             }
  97.         }
  98.     }
  99.     /**
  100.      * 商品詳細のカートボタンを変更する.
  101.      *
  102.      * @param TemplateEvent $event
  103.      */
  104.     public function onRenderProductDetail(TemplateEvent $event)
  105.     {
  106.         $parameters $event->getParameters();
  107.         $Product $parameters['Product'];
  108.         if (is_null($Product)) {
  109.             return;
  110.         }
  111.         // DBから設定情報を取得
  112.         $template $this->tbsTrialProductRepository->findOne();
  113.         // 未ログインの場合と購入履歴がある場合は対象販売種別のカートボタンを非アクティブ化
  114.         if ($template && $template['first_only'] == 1) {
  115.             $cartButtonName null;
  116.             // 未ログインの場合
  117.             if (!$this->user || $this->user == 'anon.') {
  118.                 $cartButtonName trans('tbs_trial_product.front.cart.button.login');
  119.             // 購入履歴がある場合
  120.             } elseif ($this->user->getBuyTimes() > 0) {
  121.                 $cartButtonName trans('tbs_trial_product.front.cart.button.first_only');
  122.             }
  123.             if ($cartButtonName) {
  124.                 // 規格なし商品の場合
  125.                 if (!$Product->hasProductClass()) {
  126.                     $ProductClasses $Product->getProductClasses();
  127.                     /** @var ProductClass $ProductClass */
  128.                     $ProductClass $ProductClasses[0];
  129.                     if ($template['ProductSaleType']->getId() == $ProductClass->getSaleType()->getId()) {
  130.                         $parameters $event->getParameters();
  131.                         $parameters['cartButtonName'] = $cartButtonName;
  132.                         $event->setParameters($parameters);
  133.                         $event->addSnippet('@tbsTrialProduct42/default/product_trial_detail.twig');
  134.                     }
  135.                 } // 規格あり商品の場合
  136.                 else {
  137.                     $parameters $event->getParameters();
  138.                     $parameters['ProductSaleTypeId'] = $template['ProductSaleType']->getId();
  139.                     $parameters['cartButtonName'] = $cartButtonName;
  140.                     $event->setParameters($parameters);
  141.                     $event->addSnippet('@tbsTrialProduct42/default/product_trial_detail_class.twig');
  142.                 }
  143.             }
  144.         }
  145.     }
  146.     /**
  147.      * 注文フローでゲスト購入を非表示にする.
  148.      *
  149.      * @param TemplateEvent $event
  150.      */
  151.     public function onRenderShoppingLogin(TemplateEvent $event)
  152.     {
  153.         // DBから設定情報を取得
  154.         $template $this->tbsTrialProductRepository->findOne();
  155.         // 会員登録必須の場合はゲスト購入を非表示
  156.         if ($template && $template['registration_required'] == 1) {
  157.             $event->addSnippet('@tbsTrialProduct42/default/product_trial_login.twig');
  158.         }
  159.     }
  160.     /**
  161.      * 再注文を不可にする.
  162.      *
  163.      * @param TemplateEvent $event
  164.      */
  165.     public function onRenderMypageHistory(TemplateEvent $event)
  166.     {
  167.         $event->addSnippet('@tbsTrialProduct42/default/product_trial_mypage_history.twig');
  168.     }
  169.     /**
  170.      * @return array
  171.      */
  172.     public static function getSubscribedEvents()
  173.     {
  174.         return [
  175.             // 'front.product.index.search' => 'onRenderProductIndexSearch',
  176.             'Product/list.twig' => 'onRenderProductList',
  177.             'Product/detail.twig' => 'onRenderProductDetail',
  178.             'Shopping/login.twig' => 'onRenderShoppingLogin',
  179.             'Mypage/history.twig' => 'onRenderMypageHistory',
  180.         ];
  181.     }
  182. }