app/Plugin/QuantityDiscountDx/EventSubscriber/CartEventSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/03/27
  5.  */
  6. namespace Plugin\QuantityDiscountDx\EventSubscriber;
  7. use Eccube\Entity\BaseInfo;
  8. use Eccube\Entity\Cart;
  9. use Eccube\Entity\CartItem;
  10. use Eccube\Event\TemplateEvent;
  11. use Eccube\Repository\BaseInfoRepository;
  12. use Plugin\QuantityDiscountDx\Service\ConfigService;
  13. use Plugin\QuantityDiscountDx\Service\TwigRenderService\TwigRenderService;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CartEventSubscriber implements EventSubscriberInterface
  16. {
  17.     /** @var TwigRenderService  */
  18.     protected $twigRenderService;
  19.     /** @var ConfigService  */
  20.     protected $configService;
  21.     /** @var BaseInfo  */
  22.     protected $baseInfo;
  23.     public function __construct(
  24.         TwigRenderService $twigRenderService,
  25.         ConfigService $configService,
  26.         BaseInfoRepository $baseInfoRepository
  27.     )
  28.     {
  29.         $this->twigRenderService $twigRenderService;
  30.         $this->configService $configService;
  31.         $this->baseInfo $baseInfoRepository->get();
  32.     }
  33.     /**
  34.      * カート テンプレート
  35.      *
  36.      * @param TemplateEvent $event
  37.      */
  38.     public function onTemplateCartIndex(TemplateEvent $event)
  39.     {
  40.         if (ConfigService::DISCOUNT_BEFORE_PRICE
  41.             == $this->configService->getKeyInteger(ConfigService::SETTING_KEY_DELIVERY_FEE_MODE)) {
  42.             $Carts $event->getParameter('Carts');
  43.             $least $event->getParameter('least');
  44.             $isDeliveryFree $event->getParameter('is_delivery_free');
  45.             /** @var Cart $Cart */
  46.             foreach ($Carts as $Cart) {
  47.                 if ($this->baseInfo->getDeliveryFreeAmount()) {
  48.                     $subTotal 0;
  49.                     /** @var CartItem $cartItem */
  50.                     foreach ($Cart->getCartItems() as $cartItem) {
  51.                         $subTotal += $cartItem->getProductClass()->getPrice02IncTax() * $cartItem->getQuantity();
  52.                     }
  53.                     if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $subTotal) {
  54.                         $isDeliveryFree[$Cart->getCartKey()] = true;
  55.                     } else {
  56.                         $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $subTotal;
  57.                     }
  58.                 }
  59.             }
  60.             $event->setParameter('least'$least);
  61.             $event->setParameter('is_delivery_free'$isDeliveryFree);
  62.         }
  63.         if($this->configService->isKeyBool(ConfigService::SETTING_KEY_CART_VIEW)) {
  64.             $this->twigRenderService->initRenderService($event);
  65.             $eachChild $this->twigRenderService
  66.                 ->eachChildBuilder()
  67.                 ->findThis()
  68.                 ->targetFindAndIndexKey('#quantity_discount_''qdIndex')
  69.                 ->setInsertModeAppend();
  70.             $this->twigRenderService
  71.                 ->eachBuilder()
  72.                 ->find('.ec-cartRow')
  73.                 ->find('.ec-cartRow__summary')
  74.                 ->setEachIndexKey('qdIndex')
  75.                 ->each($eachChild->build());
  76.             $this->twigRenderService->addSupportSnippet('@QuantityDiscountDx/default/Cart/index_ex.twig');
  77.         }
  78.     }
  79.     /**
  80.      * Returns an array of event names this subscriber wants to listen to.
  81.      *
  82.      * The array keys are event names and the value can be:
  83.      *
  84.      *  * The method name to call (priority defaults to 0)
  85.      *  * An array composed of the method name to call and the priority
  86.      *  * An array of arrays composed of the method names to call and respective
  87.      *    priorities, or 0 if unset
  88.      *
  89.      * For instance:
  90.      *
  91.      *  * ['eventName' => 'methodName']
  92.      *  * ['eventName' => ['methodName', $priority]]
  93.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  94.      *
  95.      * @return array The event names to listen to
  96.      */
  97.     public static function getSubscribedEvents()
  98.     {
  99.         return [
  100.             'Cart/index.twig' => ['onTemplateCartIndex']
  101.         ];
  102.     }
  103. }