app/Plugin/QuantityDiscountDx/EventSubscriber/MypageEventSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright(c) 2019 SYSTEM_KD
  4.  * Date: 2019/03/24
  5.  */
  6. namespace Plugin\QuantityDiscountDx\EventSubscriber;
  7. use Eccube\Entity\Order;
  8. use Eccube\Entity\OrderItem;
  9. use Eccube\Entity\ProductClass;
  10. use Eccube\Entity\Shipping;
  11. use Eccube\Event\TemplateEvent;
  12. use Plugin\QuantityDiscountDx\Service\ConfigService;
  13. use Plugin\QuantityDiscountDx\Service\PurchaseFlow\Processor\QuantityDiscountDiscountProcessor;
  14. use Plugin\QuantityDiscountDx\Service\QuantityDiscountService;
  15. use Plugin\QuantityDiscountDx\Service\TwigRenderService\TwigRenderService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class MypageEventSubscriber implements EventSubscriberInterface
  18. {
  19.     /** @var QuantityDiscountService */
  20.     protected $quantityDiscountService;
  21.     /** @var TwigRenderService */
  22.     protected $twigRenderService;
  23.     /** @var ConfigService */
  24.     protected $configService;
  25.     public function __construct(
  26.         QuantityDiscountService $quantityDiscountService,
  27.         TwigRenderService $twigRenderService,
  28.         ConfigService $configService
  29.     )
  30.     {
  31.         $this->quantityDiscountService $quantityDiscountService;
  32.         $this->twigRenderService $twigRenderService;
  33.         $this->configService $configService;
  34.     }
  35.     /**
  36.      * MYページ注文履歴 テンプレート
  37.      *
  38.      * @param TemplateEvent $event
  39.      */
  40.     public function onTemplateMypageHistory(TemplateEvent $event)
  41.     {
  42.         $this->twigRenderService->initRenderService($event);
  43.         // 値引き額取得
  44.         $quantityDiscounts = [];
  45.         /** @var Order $Order */
  46.         $Order $event->getParameter('Order');
  47.         /** @var OrderItem $orderItem */
  48.         foreach ($Order->getItems() as $orderItem) {
  49.             if ($orderItem->getProcessorName() == QuantityDiscountDiscountProcessor::class) {
  50.                 $shipping_id $orderItem->getShipping()->getId();
  51.                 $product_class_id $orderItem->getProductClass()->getId();
  52.                 $key $shipping_id '-' $product_class_id;
  53.                 $quantityDiscounts[$key] = abs($orderItem->getPriceIncTax() * $orderItem->getQuantity());
  54.             }
  55.         }
  56.         $event->setParameter('QuantityDiscounts'$quantityDiscounts);
  57.         $childes = [];
  58.         if ($this->configService->isKeyBool(ConfigService::SETTING_KEY_HISTORY_VIEW)) {
  59.             $childes[] = $this->twigRenderService
  60.                 ->eachChildBuilder()
  61.                 ->findThis()
  62.                 ->find('.ec-imageGrid__content')
  63.                 ->targetFindAndIndexKey('#quantity_discount_history_'"qdIndex")
  64.                 ->setInsertModeAppend();
  65.             $childes[] = $this->twigRenderService
  66.                 ->eachChildBuilder()
  67.                 ->findThis()
  68.                 ->find('.ec-imageGrid__content')
  69.                 ->targetFindAndIndexKey('#quantity_discount_'"qdIndex")
  70.                 ->setInsertModeAppend();
  71.         }
  72.         if (count($quantityDiscounts) == 0) {
  73.             // 現在価格の調整
  74.             $childes[] = $this->twigRenderService
  75.                 ->eachChildBuilder()
  76.                 ->findThis()
  77.                 ->find('.ec-imageGrid__content')
  78.                 ->find(".ec-color-accent:contains(\"現在価格\")")
  79.                 ->eq(0)
  80.                 ->targetFindAndIndexKey('#replace_quantity_discount_history_'"qdIndex")
  81.                 ->setInsertModeReplaceWith();
  82.         } else {
  83.             // まとめ買い現在価格の追加
  84.             $childes[] = $this->twigRenderService
  85.                 ->eachChildBuilder()
  86.                 ->findThis()
  87.                 ->find('.ec-imageGrid__content')
  88.                 ->eq(0)
  89.                 ->targetFindAndIndexKey('#replace_quantity_discount_history_'"qdIndex")
  90.                 ->setInsertModeAppend();
  91.         }
  92.         $this->twigRenderService
  93.             ->eachBuilder()
  94.             ->find('.ec-orderDelivery__item')
  95.             ->setEachIndexKey('qdIndex')
  96.             ->each($childes);
  97.         // 注意文言調整
  98.         // 削除用
  99.         $this->twigRenderService
  100.             ->insertBuilder()
  101.             ->find('.ec-orderRole__summary')
  102.             ->find('.ec-color-accent')
  103.             ->setTargetId('qd_no_diff')
  104.             ->setInsertModeReplaceWith();
  105.         // 追加用
  106.         $this->twigRenderService
  107.             ->insertBuilder()
  108.             ->find('.ec-orderRole__summary')
  109.             ->setTargetId('qd_in_diff')
  110.             ->setInsertModeAppend();
  111.         $this->twigRenderService->addSupportSnippet('@QuantityDiscountDx/default/Mypage/history_ex.twig');
  112.     }
  113.     public function onTemplateMypageHistoryChangePrice(TemplateEvent $event)
  114.     {
  115.         /** @var Order $Order */
  116.         $Order $event->getParameter('Order');
  117.         /** @var Shipping $Shipping */
  118.         foreach ($Order->getShippings() as $Shipping) {
  119.             /** @var OrderItem $orderItem */
  120.             foreach ($Shipping->getOrderItems() as $orderItem) {
  121.                 /** @var ProductClass $ProductClass */
  122.                 $ProductClass $orderItem->getProductClass();
  123.                 if ($ProductClass) {
  124.                     // まとめ買い値引額(税込み)
  125.                     $discountPrice $this->quantityDiscountService->getProductClassPriceIncTax($orderItem);
  126.                     if ($discountPrice >= 0) {
  127.                         $ProductClass->setPrice02IncTax($discountPrice);
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.     }
  133.     /**
  134.      * Returns an array of event names this subscriber wants to listen to.
  135.      *
  136.      * The array keys are event names and the value can be:
  137.      *
  138.      *  * The method name to call (priority defaults to 0)
  139.      *  * An array composed of the method name to call and the priority
  140.      *  * An array of arrays composed of the method names to call and respective
  141.      *    priorities, or 0 if unset
  142.      *
  143.      * For instance:
  144.      *
  145.      *  * ['eventName' => 'methodName']
  146.      *  * ['eventName' => ['methodName', $priority]]
  147.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  148.      *
  149.      * @return array The event names to listen to
  150.      */
  151.     public static function getSubscribedEvents()
  152.     {
  153.         return [
  154.             'Mypage/history.twig' => [
  155.                 ['onTemplateMypageHistory'],
  156. //                ['onTemplateMypageHistoryChangePrice', -1024],
  157.             ]
  158.         ];
  159.     }
  160. }