app/Plugin/payjp4/EventSubscriber/AddCartEventSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of payjp4
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  *  https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\payjp4\EventSubscriber;
  13. use Eccube\Entity\Order;
  14. use Eccube\Entity\Product;
  15. use Eccube\Entity\ProductClass;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Exception\CartException;
  19. use Eccube\Service\CartService;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. class AddCartEventSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var CartService
  26.      */
  27.     private $cartService;
  28.     public function __construct(
  29.         CartService $cartService
  30.     )
  31.     {
  32.         $this->cartService $cartService;
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         // TODO: Implement getSubscribedEvents() method.
  40.         return [
  41.             EccubeEvents::FRONT_PRODUCT_CART_ADD_INITIALIZE => 'onFrontProductCartAddInitialize',
  42.             EccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE => 'onFrontMypageMypageOrderInitialize'
  43.         ];
  44.     }
  45.     /**
  46.      * 定期購入商品をカートに追加したときは追加前にカートクリア
  47.      *
  48.      * @param EventArgs $args
  49.      */
  50.     public function onFrontProductCartAddInitialize(EventArgs $args): void
  51.     {
  52.         /** @var Product $Product */
  53.         $Product $args->getArgument('Product');
  54.         /** @var ProductClass $productClass */
  55.         foreach($Product->getProductClasses() as $productClass)
  56.         {
  57.             if($productClass->getSaleType()->getName() === trans('plugin.payjp.admin.sale_type.name')) {
  58.                 $this->cartService->clear();
  59.             }
  60.         }
  61.     }
  62.     /**
  63.      * 定期購入商品は再注文できない
  64.      *
  65.      * @param EventArgs $args
  66.      * @throws CartException
  67.      */
  68.     public function onFrontMypageMypageOrderInitialize(EventArgs $args): void
  69.     {
  70.         /** @var Order $Order */
  71.         $Order $args->getArgument('Order');
  72.         if(!$Order) {
  73.             return;
  74.         }
  75.         foreach($Order->getSaleTypes() as $saleType) {
  76.             if($saleType->getName() === trans('plugin.payjp.admin.sale_type.name')) {
  77.                 log_info('定期購入商品は再注文できません');
  78.                 throw new NotFoundHttpException();
  79.             }
  80.         }
  81.     }
  82. }