app/Plugin/PinpointSaleDx/EventSubscriber/CartEventSubscriber.php line 154

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/08/18
  5.  */
  6. namespace Plugin\PinpointSaleDx\EventSubscriber;
  7. use Doctrine\Common\Annotations\Annotation\Required;
  8. use Eccube\Entity\BaseInfo;
  9. use Eccube\Entity\Cart;
  10. use Eccube\Entity\CartItem;
  11. use Eccube\Event\TemplateEvent;
  12. use Eccube\Repository\BaseInfoRepository;
  13. use Plugin\PinpointSaleDx\Config\ConfigSetting;
  14. use Plugin\PinpointSaleDx\Config\ConfigSupportTrait;
  15. use Plugin\PinpointSaleDx\Service\PlgConfigService\ConfigService;
  16. use Plugin\PinpointSaleDx\Service\TwigRenderService\TwigRenderService;
  17. use Plugin\PinpointSaleDx\Service\PinpointSaleDxService;
  18. use Plugin\PinpointSaleDx\Service\PluginLinkService;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class CartEventSubscriber implements EventSubscriberInterface
  21. {
  22.     /** @var TwigRenderService */
  23.     protected $twigRenderService;
  24.     /** @var ConfigService */
  25.     protected $configService;
  26.     /** @var BaseInfo */
  27.     protected $baseInfo;
  28.     /** @var PinpointSaleDxService */
  29.     protected $pinpointSaleService;
  30.     /** @var PluginLinkService */
  31.     protected $pluginLinkService;
  32.     /** @var \Plugin\QuantityDiscountDx\Service\ConfigService */
  33.     protected $quantityDiscountConfigService;
  34.     use ConfigSupportTrait;
  35.     /**
  36.      * CartEventSubscriber constructor.
  37.      * @param TwigRenderService $twigRenderService
  38.      * @param ConfigService $configService
  39.      * @param BaseInfoRepository $baseInfoRepository
  40.      * @param PinpointSaleDxService $pinpointSaleService
  41.      * @param PluginLinkService $pluginLinkService
  42.      * @throws \Exception
  43.      */
  44.     public function __construct(
  45.         TwigRenderService   $twigRenderService,
  46.         ConfigService       $configService,
  47.         BaseInfoRepository  $baseInfoRepository,
  48.         PinpointSaleDxService $pinpointSaleService,
  49.         PluginLinkService   $pluginLinkService
  50.     )
  51.     {
  52.         $this->twigRenderService $twigRenderService;
  53.         $this->configService $configService;
  54.         $this->baseInfo $baseInfoRepository->get();
  55.         $this->pinpointSaleService $pinpointSaleService;
  56.         $this->pluginLinkService $pluginLinkService;
  57.     }
  58.     /**
  59.      * @Required
  60.      *
  61.      * @param \Plugin\QuantityDiscountDx\Service\ConfigService|null $configService
  62.      * @return void
  63.      */
  64.     public function setQuantityDiscountConfig(?\Plugin\QuantityDiscountDx\Service\ConfigService $configService)
  65.     {
  66.         $this->quantityDiscountConfigService $configService;
  67.     }
  68.     /**
  69.      * カートテンプレート
  70.      *
  71.      * @param TemplateEvent $event
  72.      */
  73.     public function onTemplateCartIndex(TemplateEvent $event)
  74.     {
  75.         // 送料無料判別設定
  76.         if (Configsetting::DISCOUNT_BEFORE_PRICE
  77.             == $this->configService->getKeyInteger(Configsetting::SETTING_KEY_DELIVERY_FEE_MODE)) {
  78.             $Carts $event->getParameter('Carts');
  79.             $least $event->getParameter('least');
  80.             $isDeliveryFree $event->getParameter('is_delivery_free');
  81.             /** @var Cart $Cart */
  82.             foreach ($Carts as $Cart) {
  83.                 if ($this->baseInfo->getDeliveryFreeAmount()) {
  84.                     $subTotal 0;
  85.                     /** @var CartItem $cartItem */
  86.                     foreach ($Cart->getCartItems() as $cartItem) {
  87.                         $pinpointSaleItem $this->pinpointSaleService->getPinpointSaleDxItem($cartItem->getProductClass());
  88.                         $subTotal += $cartItem->getPriceIncTax() * $cartItem->getQuantity();
  89.                         if ($pinpointSaleItem) {
  90.                             // タイムセール値引き分
  91.                             $discount $pinpointSaleItem->getDiscountPriceIncTax() * $cartItem->getQuantity();
  92.                             $subTotal += $discount;
  93.                         }
  94.                     }
  95.                     if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $subTotal) {
  96.                         $isDeliveryFree[$Cart->getCartKey()] = true;
  97.                     } else {
  98.                         $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $subTotal;
  99.                     }
  100.                 }
  101.             }
  102.             $event->setParameter('least'$least);
  103.             $event->setParameter('is_delivery_free'$isDeliveryFree);
  104.         }
  105.         // タイムセール名称設定
  106.         $this->setDiscountName($event);
  107.         if (!$this->configService->isKeyBool(ConfigSetting::SETTING_KEY_CART_VIEW)) {
  108.             return;
  109.         }
  110.         $this->twigRenderService->initRenderService($event);
  111.         $eachChild $this->twigRenderService
  112.             ->eachChildBuilder()
  113.             ->findThis()
  114.             ->targetFindAndIndexKey('#pinpoint_sale_price_''pinpointSaleIndex')
  115.             ->setInsertModeAppend();
  116.         $this->twigRenderService
  117.             ->eachBuilder()
  118.             ->find('.ec-cartRow')
  119.             ->find('.ec-cartRow__summary')
  120.             ->setEachIndexKey('pinpointSaleIndex')
  121.             ->each($eachChild->build());
  122.         $this->twigRenderService->addSupportSnippet(
  123.             '@PinpointSaleDx/default/Cart/index_ex.twig'
  124.         );
  125.     }
  126.     public function onCartIndexHook(TemplateEvent $event)
  127.     {
  128.         // まとめ買いプラグインとの競合解消
  129.         if ($this->pluginLinkService->isActivePlugin('QuantityDiscountDx')) {
  130.             if ((Configsetting::DISCOUNT_BEFORE_PRICE
  131.                     == $this->configService->getKeyInteger(Configsetting::SETTING_KEY_DELIVERY_FEE_MODE))
  132.                 &&
  133.                 ($this->quantityDiscountConfigService::DISCOUNT_BEFORE_PRICE
  134.                     == $this->quantityDiscountConfigService->getKeyInteger($this->quantityDiscountConfigService::SETTING_KEY_DELIVERY_FEE_MODE))) {
  135.                 $Carts $event->getParameter('Carts');
  136.                 $least $event->getParameter('least');
  137.                 $isDeliveryFree $event->getParameter('is_delivery_free');
  138.                 /** @var Cart $Cart */
  139.                 foreach ($Carts as $Cart) {
  140.                     if ($this->baseInfo->getDeliveryFreeAmount()) {
  141.                         $subTotal 0;
  142.                         /** @var CartItem $cartItem */
  143.                         foreach ($Cart->getCartItems() as $cartItem) {
  144.                             $pinpointSaleItem $this->pinpointSaleService->getPinpointSaleDxItem($cartItem->getProductClass());
  145.                             if ($pinpointSaleItem) {
  146.                                 // タイムセール値引き分
  147.                                 $subTotal += $cartItem->getProductClass()->getPrice02IncTax() * $cartItem->getQuantity();
  148.                                 $discount $pinpointSaleItem->getDiscountPriceIncTax() * $cartItem->getQuantity();
  149.                                 $subTotal += $discount;
  150.                             } else {
  151.                                 $subTotal += $cartItem->getPrice() * $cartItem->getQuantity();
  152.                             }
  153.                         }
  154.                         if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $subTotal) {
  155.                             $isDeliveryFree[$Cart->getCartKey()] = true;
  156.                         } else {
  157.                             $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $subTotal;
  158.                         }
  159.                     }
  160.                 }
  161.                 $event->setParameter('least'$least);
  162.                 $event->setParameter('is_delivery_free'$isDeliveryFree);
  163.             }
  164.         }
  165.     }
  166.     /**
  167.      * Returns an array of event names this subscriber wants to listen to.
  168.      *
  169.      * The array keys are event names and the value can be:
  170.      *
  171.      *  * The method name to call (priority defaults to 0)
  172.      *  * An array composed of the method name to call and the priority
  173.      *  * An array of arrays composed of the method names to call and respective
  174.      *    priorities, or 0 if unset
  175.      *
  176.      * For instance:
  177.      *
  178.      *  * ['eventName' => 'methodName']
  179.      *  * ['eventName' => ['methodName', $priority]]
  180.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  181.      *
  182.      * @return array The event names to listen to
  183.      */
  184.     public static function getSubscribedEvents()
  185.     {
  186.         return [
  187.             'Cart/index.twig' => [
  188.                 ['onTemplateCartIndex'],
  189.                 ['onCartIndexHook', -10],
  190.             ],
  191.         ];
  192.     }
  193. }