<?php
namespace Plugin\ApexOrderItemOption\Event;
use Eccube\Event\TemplateEvent;
use Plugin\ApexOrderItemOption\Repository\OptionCategoryRepository;
use Plugin\ApexOrderItemOption\Service\OptionPriceCalculator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MypageEvent implements EventSubscriberInterface
{
private $optionCategoryRepository;
private $optionPriceCalculator;
public function __construct(
OptionCategoryRepository $optionCategoryRepository,
OptionPriceCalculator $optionPriceCalculator
){
$this->optionCategoryRepository = $optionCategoryRepository;
$this->optionPriceCalculator = $optionPriceCalculator;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Mypage/index.twig' => 'onTemplateMypageIndex',
'Mypage/history.twig' => 'onTemplateMypageHistory',
];
}
public function onTemplateMypageIndex(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/Order\.MergedProductOrderItems/",$source, $result)){
$search = $result[0];
$replace = "Order.MergedProductOptionOrderItems";
$source = str_replace($search, $replace, $source);
}
if(preg_match("/\<p\sclass\=\"ec\-historyRole\_\_detailPrice\"\>/",$source, $result)){
$search = $result[0];
$replace = $search . "{{ include('@ApexOrderItemOption/default/Mypage/orderitem_option.twig') }}";
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
}
public function onTemplateMypageHistory(TemplateEvent $event)
{
$parameters = $event->getParameters();
$Order = $parameters['Order'];
foreach($Order->getProductOrderItems() as $OrderItem){
$current_price = 0;
foreach($OrderItem->getOrderItemOptions() as $OrderItemOption){
foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
$optionCategoryId = $OrderItemOptionCategory->getOptionCategoryId();
if($optionCategoryId > 0){
$OptionCategory = $this->optionCategoryRepository->find($optionCategoryId);
if(!is_null($OptionCategory)){
$current_price += $this->optionPriceCalculator->getPriceIncTax($OptionCategory->getOption(), $OptionCategory, $OrderItemOptionCategory->getValue());
}
}
}
}
// オプション料金は税込で登録されているため、現在価格はそのまま税込額として扱う
$OrderItem->setCurrentPrice($current_price);
$OrderItem->setCurrentTax(0);
}
$event->setParameters($parameters);
$source = $event->getSource();
if(preg_match("/=\s*orderItem\.productClass\.price02IncTax|=\s*orderItem\.productClass\.customer\_rank\_priceIncTax/",$source, $result)){
$search = $result[0];
$replace = $search . "+ orderItem.CurrentPriceIncTax";
$source = str_replace($search, $replace, $source);
}
if(preg_match("/\{\{\s*orderItem\.productClass\.price02IncTax\|price\s*\}\}/",$source, $result)){
$search = $result[0];
$replace = "{% set currentPriceIncTax = orderItem.productClass.price02IncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
$source = str_replace($search, $replace, $source);
}
if(preg_match("/\{\{\s*orderItem\.productClass\.customer_rank_priceIncTax\|price\s*\}\}/",$source, $result)){
$search = $result[0];
$replace = "{% set currentPriceIncTax = orderItem.productClass.customer_rank_priceIncTax+orderItem.CurrentPriceIncTax %}{{ currentPriceIncTax|price }}";
$source = str_replace($search, $replace, $source);
}
if(preg_match("/\<p\>\{\{\sorderItem\.price\_inc\_tax\|price\s\}\}/",$source, $result)){
$search = $result[0];
$replace = "{{ include('@ApexOrderItemOption/default/Shopping/orderitem_option.twig') }}" . $search;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
}
}