app/Plugin/ApexOrderItemOption/Event/MypageEvent.php line 55

Open in your IDE?
  1. <?php
  2. namespace Plugin\ApexOrderItemOption\Event;
  3. use Eccube\Event\TemplateEvent;
  4. use Plugin\ApexOrderItemOption\Repository\OptionCategoryRepository;
  5. use Plugin\ApexOrderItemOption\Service\OptionPriceCalculator;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class MypageEvent implements EventSubscriberInterface
  8. {
  9.     private $optionCategoryRepository;
  10.     private $optionPriceCalculator;
  11.     public function __construct(
  12.         OptionCategoryRepository $optionCategoryRepository,
  13.         OptionPriceCalculator $optionPriceCalculator
  14.     ){
  15.         $this->optionCategoryRepository = $optionCategoryRepository;
  16.         $this->optionPriceCalculator = $optionPriceCalculator;
  17.     }
  18.     /**
  19.      * @return array
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             'Mypage/index.twig' => 'onTemplateMypageIndex',
  25.             'Mypage/history.twig' => 'onTemplateMypageHistory',
  26.         ];
  27.     }
  28.     public function onTemplateMypageIndex(TemplateEvent $event)
  29.     {
  30.         $source = $event->getSource();
  31.         if(preg_match("/Order\.MergedProductOrderItems/",$source, $result)){
  32.             $search = $result[0];
  33.             $replace = "Order.MergedProductOptionOrderItems";
  34.             $source = str_replace($search, $replace, $source);
  35.         }
  36.         if(preg_match("/\<p\sclass\=\"ec\-historyRole\_\_detailPrice\"\>/",$source, $result)){
  37.             $search = $result[0];
  38.             $replace = $search . "{{ include('@ApexOrderItemOption/default/Mypage/orderitem_option.twig') }}";
  39.             $source = str_replace($search, $replace, $source);
  40.         }
  41.         $event->setSource($source);
  42.     }
  43.     public function onTemplateMypageHistory(TemplateEvent $event)
  44.     {
  45.         $parameters = $event->getParameters();
  46.         $Order = $parameters['Order'];
  47.         foreach($Order->getProductOrderItems() as $OrderItem){
  48.             $current_price = 0;
  49.             foreach($OrderItem->getOrderItemOptions() as $OrderItemOption){
  50.                 foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
  51.                     $optionCategoryId = $OrderItemOptionCategory->getOptionCategoryId();
  52.                     if($optionCategoryId > 0){
  53.                         $OptionCategory = $this->optionCategoryRepository->find($optionCategoryId);
  54.                         if(!is_null($OptionCategory)){
  55.                             $current_price += $this->optionPriceCalculator->getPriceIncTax($OptionCategory->getOption(), $OptionCategory, $OrderItemOptionCategory->getValue());
  56.                         }
  57.                     }
  58.                 }
  59.             }
  60.             // オプション料金は税込で登録されているため、現在価格はそのまま税込額として扱う
  61.             $OrderItem->setCurrentPrice($current_price);
  62.             $OrderItem->setCurrentTax(0);
  63.         }
  64.         $event->setParameters($parameters);
  65.         $source = $event->getSource();
  66.         if(preg_match("/=\s*orderItem\.productClass\.price02IncTax|=\s*orderItem\.productClass\.customer\_rank\_priceIncTax/",$source, $result)){
  67.             $search = $result[0];
  68.             $replace = $search . "+ orderItem.CurrentPriceIncTax";
  69.             $source = str_replace($search, $replace, $source);
  70.         }
  71.         if(preg_match("/\{\{\s*orderItem\.productClass\.price02IncTax\|price\s*\}\}/",$source, $result)){
  72.             $search = $result[0];
  73.             $replace = "{% set currentPriceIncTax = orderItem.productClass.price02IncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
  74.             $source = str_replace($search, $replace, $source);
  75.         }
  76.         if(preg_match("/\{\{\s*orderItem\.productClass\.customer_rank_priceIncTax\|price\s*\}\}/",$source, $result)){
  77.             $search = $result[0];
  78.             $replace = "{% set currentPriceIncTax = orderItem.productClass.customer_rank_priceIncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
  79.             $source = str_replace($search, $replace, $source);
  80.         }
  81.         if(preg_match("/\<p\>\{\{\sorderItem\.price\_inc\_tax\|price\s\}\}/",$source, $result)){
  82.             $search = $result[0];
  83.             $replace = "{{ include('@ApexOrderItemOption/default/Shopping/orderitem_option.twig') }}" . $search;
  84.             $source = str_replace($search, $replace, $source);
  85.         }
  86.         $event->setSource($source);
  87.     }
  88. }