app/Plugin/ProductOption42/Event/CustomMypageHistoryIndexEvent.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ProductOption
  4. *
  5. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  6. * http://www.bratech.co.jp/
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. ファイル名:CustomMypageHistoryIndexEvent.php
  11. 処理概要:トップページの注文履歴を注文履歴一覧ページに引っ越した為、商品オプションの情報を注文履歴一覧ページに表示させるための調整
  12. */
  13. namespace Plugin\ProductOption42\Event;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Service\TaxRuleService;
  16. use Plugin\ProductOption42\Entity\Option;
  17. use Plugin\ProductOption42\Repository\OptionCategoryRepository;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class CustomMypageHistoryIndexEvent implements EventSubscriberInterface
  20. //class MypageEvent implements EventSubscriberInterface
  21. {
  22.     private $optionCategoryRepository;
  23.     private $taxRuleService;
  24.     public function __construct(
  25.         OptionCategoryRepository $optionCategoryRepository,
  26.         TaxRuleService $taxRuleService
  27.     ) {
  28.         $this->optionCategoryRepository $optionCategoryRepository;
  29.         $this->taxRuleService $taxRuleService;
  30.     }
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             'Mypage/history_index.twig' => 'onTemplateMypageIndex',
  38.             //'Mypage/index.twig' => 'onTemplateMypageIndex',
  39.             //'Mypage/history.twig' => 'onTemplateMypageHistory',
  40.         ];
  41.     }
  42.     public function onTemplateMypageIndex(TemplateEvent $event)
  43.     {
  44.         $source $event->getSource();
  45.         if (preg_match("/Order\.MergedProductOrderItems/"$source$result)) {
  46.             $search $result[0];
  47.             $replace "Order.MergedProductOptionOrderItems";
  48.             $source str_replace($search$replace$source);
  49.         }
  50.         if (preg_match("/\<p\sclass\=\"ec\-historyRole\_\_detailPrice\"\>/"$source$result)) {
  51.             $search $result[0];
  52.             $replace $search "{{ include('@ProductOption42/default/Mypage/orderitem_option.twig') }}";
  53.             $source str_replace($search$replace$source);
  54.         }
  55.         $event->setSource($source);
  56.     }
  57.     public function onTemplateMypageHistory(TemplateEvent $event)
  58.     {
  59.         $parameters $event->getParameters();
  60.         $Order $parameters['Order'];
  61.         foreach ($Order->getProductOrderItems() as $OrderItem) {
  62.             $ProductClass $OrderItem->getProductClass();
  63.             $current_price 0;
  64.             foreach ($OrderItem->getOrderItemOptions() as $OrderItemOption) {
  65.                 foreach ($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory) {
  66.                     $optionCategoryId $OrderItemOptionCategory->getOptionCategoryId();
  67.                     if ($optionCategoryId 0) {
  68.                         $OptionCategory $this->optionCategoryRepository->find($optionCategoryId);
  69.                         if (!is_null($OptionCategory)) {
  70.                             $option_price $OptionCategory->getValue();
  71.                             if ($OptionCategory->getOption()->getType() == Option::NUMBER_TYPE && $OptionCategory->getMultipleFlg()) {
  72.                                 $option_price *= $OrderItemOptionCategory->getValue();
  73.                             }
  74.                             $current_price += $option_price;
  75.                         }
  76.                     }
  77.                 }
  78.             }
  79.             $OrderItem->setCurrentPrice($current_price);
  80.             $OrderItem->setCurrentTax($this->taxRuleService->getTax($OrderItem->getCurrentPrice(), $ProductClass->getProduct(), $ProductClass));
  81.         }
  82.         $event->setParameters($parameters);
  83.         $source $event->getSource();
  84.         if (preg_match("/=\s*orderItem\.productClass\.price02IncTax|=\s*orderItem\.productClass\.customer\_rank\_priceIncTax/"$source$result)) {
  85.             $search $result[0];
  86.             $replace $search "+ orderItem.CurrentPriceIncTax";
  87.             $source str_replace($search$replace$source);
  88.         }
  89.         if (preg_match("/\{\{\s*orderItem\.productClass\.price02IncTax\|price\s*\}\}/"$source$result)) {
  90.             $search $result[0];
  91.             $replace "{% set currentPriceIncTax = orderItem.productClass.price02IncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
  92.             $source str_replace($search$replace$source);
  93.         }
  94.         if (preg_match("/\{\{\s*orderItem\.productClass\.customer_rank_priceIncTax\|price\s*\}\}/"$source$result)) {
  95.             $search $result[0];
  96.             $replace "{% set currentPriceIncTax = orderItem.productClass.customer_rank_priceIncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
  97.             $source str_replace($search$replace$source);
  98.         }
  99.         if (preg_match("/\<p\>\{\{\sorderItem\.price\_inc\_tax\|price\s\}\}/"$source$result)) {
  100.             $search $result[0];
  101.             $replace "{{ include('@ProductOption42/default/Shopping/orderitem_option.twig') }}" $search;
  102.             $source str_replace($search$replace$source);
  103.         }
  104.         $event->setSource($source);
  105.     }
  106. }