app/Plugin/UnderLimitQuantityDx/EventSubscriber/ProductEventSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/10/27
  5.  */
  6. namespace Plugin\UnderLimitQuantityDx\EventSubscriber;
  7. use Eccube\Entity\Product;
  8. use Eccube\Entity\ProductClass;
  9. use Eccube\Event\EccubeEvents;
  10. use Eccube\Event\EventArgs;
  11. use Eccube\Event\TemplateEvent;
  12. use Plugin\UnderLimitQuantityDx\Service\TwigRenderService\EventSubscriber\TwigRenderTrait;
  13. use Plugin\UnderLimitQuantityDx\Service\UnderLimitQuantityHelper;
  14. use Plugin\UnderLimitQuantityDx\Service\UnderLimitQuantityService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Form\FormInterface;
  17. class ProductEventSubscriber implements EventSubscriberInterface
  18. {
  19.     use TwigRenderTrait;
  20.     /** @var UnderLimitQuantityService */
  21.     protected $underLimitQuantityService;
  22.     /** @var UnderLimitQuantityHelper */
  23.     protected $underLimitQuantityHelper;
  24.     public function __construct(
  25.         UnderLimitQuantityService $underLimitQuantityService,
  26.         UnderLimitQuantityHelper $underLimitQuantityHelper
  27.     )
  28.     {
  29.         $this->underLimitQuantityService $underLimitQuantityService;
  30.         $this->underLimitQuantityHelper $underLimitQuantityHelper;
  31.     }
  32.     /**
  33.      * 商品一覧
  34.      *
  35.      * @param TemplateEvent $event
  36.      * @throws \Twig_Error_Loader
  37.      * @throws \Twig_Error_Runtime
  38.      * @throws \Twig_Error_Syntax
  39.      */
  40.     public function onTemplateProductList(TemplateEvent $event)
  41.     {
  42.         $this->initRenderService($event);
  43.         // 数量制御用JSの追加
  44.         $this->addTwigRenderSnippet(
  45.             null,
  46.             '@UnderLimitQuantityDx/default/Product/list_ex_js.twig'
  47.         );
  48.         // Script改変
  49.         $this->underLimitQuantityHelper->changeAddCartJS($event);
  50.     }
  51.     /**
  52.      * 商品詳細
  53.      *
  54.      * @param TemplateEvent $event
  55.      * @throws \Twig_Error_Loader
  56.      * @throws \Twig_Error_Runtime
  57.      * @throws \Twig_Error_Syntax
  58.      */
  59.     public function onTemplateProductDetail(TemplateEvent $event)
  60.     {
  61.         $this->initRenderService($event);
  62.         // 数量制御用JSの追加
  63.         $this->addTwigRenderSnippet(
  64.             null,
  65.             '@UnderLimitQuantityDx/default/Product/detail_ex_js.twig'
  66.         );
  67.         // Script改変
  68.         $this->underLimitQuantityHelper->changeAddCartJS($event);
  69.     }
  70.     /**
  71.      * カート追加処理完了時
  72.      *
  73.      * @param EventArgs $event
  74.      */
  75.     public function onFrontProductCartAddComplete(EventArgs $event)
  76.     {
  77.         /** @var Product $product */
  78.         $product $event->getArgument('Product');
  79.         /** @var FormInterface $form */
  80.         $form $event->getArgument('form');
  81.         /** @var ProductClass $productClass */
  82.         $productClass $form->get('ProductClass')->getData();
  83.         $underQuantity $this->underLimitQuantityService->getUnderQuantity($productClass);
  84.         // 最低購入数が設定されていない場合は処理を抜ける
  85.         if ($underQuantity == 1) {
  86.             return;
  87.         }
  88.         // カートへの追加が成功しているか
  89.         $nowQuantity $this->underLimitQuantityService->getCartProductClassQuantity($productClass);
  90.         if ($nowQuantity 0) {
  91.             // 追加成功 or 既に追加されている
  92.             $underQuantity $this->underLimitQuantityService->getUnderQuantity($productClass);
  93.             if ($nowQuantity >= $underQuantity) {
  94.                 // 必要数量がセットされている
  95.                 return;
  96.             } else {
  97.                 // 最低購入数を返却
  98.                 $event->setResponse(
  99.                     $this->underLimitQuantityHelper->getAddCartNGResponse($productClass, ($underQuantity $nowQuantity))
  100.                 );
  101.             }
  102.         } else {
  103.             // カートに商品なし = 追加に失敗
  104.             // 最低購入数を返却
  105.             $event->setResponse(
  106.                 $this->underLimitQuantityHelper->getAddCartNGResponse($productClass, ($underQuantity))
  107.             );
  108.         }
  109.     }
  110.     /**
  111.      * Returns an array of event names this subscriber wants to listen to.
  112.      *
  113.      * The array keys are event names and the value can be:
  114.      *
  115.      *  * The method name to call (priority defaults to 0)
  116.      *  * An array composed of the method name to call and the priority
  117.      *  * An array of arrays composed of the method names to call and respective
  118.      *    priorities, or 0 if unset
  119.      *
  120.      * For instance:
  121.      *
  122.      *  * ['eventName' => 'methodName']
  123.      *  * ['eventName' => ['methodName', $priority]]
  124.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  125.      *
  126.      * @return array The event names to listen to
  127.      */
  128.     public static function getSubscribedEvents()
  129.     {
  130.         return [
  131.             // 商品一覧
  132.             'Product/list.twig' => ['onTemplateProductList'],
  133.             // 商品詳細
  134.             'Product/detail.twig' => ['onTemplateProductDetail'],
  135.             // カート追加完了
  136.             EccubeEvents::FRONT_PRODUCT_CART_ADD_COMPLETE => ['onFrontProductCartAddComplete', -10],
  137.         ];
  138.     }
  139. }