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

Open in your IDE?
  1. <?php
  2. namespace Plugin\ApexOrderItemOption\Event;
  3. use Eccube\Event\TemplateEvent;
  4. use Eccube\Service\TaxRuleService;
  5. use Plugin\ApexOrderItemOption\Entity\Option;
  6. use Plugin\ApexOrderItemOption\Repository\OptionCategoryRepository;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class MypageEvent implements EventSubscriberInterface
  9. {
  10.     private $optionCategoryRepository;
  11.     private $taxRuleService;
  12.     public function __construct(
  13.         OptionCategoryRepository $optionCategoryRepository,
  14.         TaxRuleService $taxRuleService
  15.     ){
  16.         $this->optionCategoryRepository $optionCategoryRepository;
  17.         $this->taxRuleService $taxRuleService;
  18.     }
  19.     /**
  20.      * @return array
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             'Mypage/index.twig' => 'onTemplateMypageIndex',
  26.             'Mypage/history.twig' => 'onTemplateMypageHistory',
  27.         ];
  28.     }
  29.     public function onTemplateMypageIndex(TemplateEvent $event)
  30.     {
  31.         $source $event->getSource();
  32.         if(preg_match("/Order\.MergedProductOrderItems/",$source$result)){
  33.             $search $result[0];
  34.             $replace "Order.MergedProductOptionOrderItems";
  35.             $source str_replace($search$replace$source);
  36.         }
  37.         if(preg_match("/\<p\sclass\=\"ec\-historyRole\_\_detailPrice\"\>/",$source$result)){
  38.             $search $result[0];
  39.             $replace $search "{{ include('@ApexOrderItemOption/default/Mypage/orderitem_option.twig') }}";
  40.             $source str_replace($search$replace$source);
  41.         }
  42.         $event->setSource($source);
  43.     }
  44.     public function onTemplateMypageHistory(TemplateEvent $event)
  45.     {
  46.         $parameters $event->getParameters();
  47.         $Order $parameters['Order'];
  48.         foreach($Order->getProductOrderItems() as $OrderItem){
  49.             $ProductClass $OrderItem->getProductClass();
  50.             $current_price 0;
  51.             foreach($OrderItem->getOrderItemOptions() as $OrderItemOption){
  52.                 foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
  53.                     $optionCategoryId $OrderItemOptionCategory->getOptionCategoryId();
  54.                     if($optionCategoryId 0){
  55.                         $OptionCategory $this->optionCategoryRepository->find($optionCategoryId);
  56.                         if(!is_null($OptionCategory)){
  57.                             $option_price $OptionCategory->getValue();
  58.                             if($OptionCategory->getOption()->getType() == Option::NUMBER_TYPE && $OptionCategory->getMultipleFlg()){
  59.                                 $option_price *= $OrderItemOptionCategory->getValue();
  60.                             }
  61.                             $current_price += $option_price;
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.             $OrderItem->setCurrentPrice($current_price);
  67.             $OrderItem->setCurrentTax($this->taxRuleService->getTax($OrderItem->getCurrentPrice(),$ProductClass->getProduct(),$ProductClass));
  68.         }
  69.         $event->setParameters($parameters);
  70.         $source $event->getSource();
  71.         if(preg_match("/=\s*orderItem\.productClass\.price02IncTax|=\s*orderItem\.productClass\.customer\_rank\_priceIncTax/",$source$result)){
  72.             $search $result[0];
  73.             $replace $search "+ orderItem.CurrentPriceIncTax";
  74.             $source str_replace($search$replace$source);
  75.         }
  76.         if(preg_match("/\{\{\s*orderItem\.productClass\.price02IncTax\|price\s*\}\}/",$source$result)){
  77.             $search $result[0];
  78.             $replace "{% set currentPriceIncTax = orderItem.productClass.price02IncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
  79.             $source str_replace($search$replace$source);
  80.         }
  81.         if(preg_match("/\{\{\s*orderItem\.productClass\.customer_rank_priceIncTax\|price\s*\}\}/",$source$result)){
  82.             $search $result[0];
  83.             $replace "{% set currentPriceIncTax = orderItem.productClass.customer_rank_priceIncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
  84.             $source str_replace($search$replace$source);
  85.         }
  86.         if(preg_match("/\<p\>\{\{\sorderItem\.price\_inc\_tax\|price\s\}\}/",$source$result)){
  87.             $search $result[0];
  88.             $replace "{{ include('@ApexOrderItemOption/default/Shopping/orderitem_option.twig') }}" $search;
  89.             $source str_replace($search$replace$source);
  90.         }
  91.         $event->setSource($source);
  92.     }
  93. }