app/Plugin/ProductOption42/Event/MypageEvent.php line 46

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