<?php
/*
* Plugin Name : ProductOption
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\ProductOption42\Event;
use Eccube\Event\TemplateEvent;
use Plugin\ProductOption42\Entity\OptionCategory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CartEvent implements EventSubscriberInterface
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Cart/index.twig' => 'onTemplateCart',
];
}
public function onTemplateCart(TemplateEvent $event)
{
$source = $event->getSource();
if(preg_match("/url\('cart\_handle\_item'\s*,\s*\{'operation'\s*:\s*'down'\s*,\s*'productClassId'\s*:\s*ProductClass\.id/",$source, $result)){
$search = $result[0];
$snipet = "url('productoption_cart_handle_item', {'operation': 'down', 'cartItemId': CartItem.id";
$replace = $snipet;
$source = str_replace($search, $replace, $source);
}
if(preg_match("/url\('cart\_handle\_item'\s*,\s*\{'operation'\s*:\s*'up'\s*,\s*'productClassId'\s*:\s*ProductClass\.id/",$source, $result)){
$search = $result[0];
$snipet = "url('productoption_cart_handle_item', {'operation': 'up', 'cartItemId': CartItem.id";
$replace = $snipet;
$source = str_replace($search, $replace, $source);
}
if(preg_match("/url\('cart\_handle\_item',\s\{'operation'\:\s'remove',\s'productClassId'\:\sProductClass\.id/",$source, $result)){
$search = $result[0];
$snipet = "url('productoption_cart_handle_item', {'operation': 'remove', 'cartItemId': CartItem.id";
$replace = $snipet;
$source = str_replace($search, $replace, $source);
}
if(preg_match("/\<\/div\>[\n|\r\n\\r]\s*<div\sclass\=\"ec\-cartRow\_\_unitPrice\"\>/",$source, $result)){
$search = $result[0];
$replace = "{{ include('@ProductOption42/default/Cart/cart_option.twig') }}" . $search;
$source = str_replace($search, $replace, $source);
}
$event->setSource($source);
$parameters = $event->getParameters();
$Carts = $parameters['Carts'];
$isDeliveryFree = $parameters['is_delivery_free'];
foreach($Carts as $Cart){
foreach($Cart->getCartItems() as $CartItem){
$flg = $CartItem->getDeliveryFreeFlg();
if($flg == OptionCategory::ON){
if(!$isDeliveryFree[$Cart->getCartKey()]){
$isDeliveryFree[$Cart->getCartKey()] = true;
}
}
}
}
$parameters['is_delivery_free'] = $isDeliveryFree;
$event->setParameters($parameters);
}
}