app/Plugin/ProductOption42/Event/CartEvent.php line 32

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. * 修正年月日:2024/05/13 k.saito
  11. * 修正内容:カート削除イベントのためのlog_info 埋込
  12. */
  13. namespace Plugin\ProductOption42\Event;
  14. use Eccube\Event\TemplateEvent;
  15. use Plugin\ProductOption42\Entity\OptionCategory;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class CartEvent implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @return array
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             'Cart/index.twig' => 'onTemplateCart',
  26.         ];
  27.     }
  28.     public function onTemplateCart(TemplateEvent $event)
  29.     {
  30.         $source = $event->getSource();
  31.         if(preg_match("/url\('cart\_handle\_item'\s*,\s*\{'operation'\s*:\s*'down'\s*,\s*'productClassId'\s*:\s*ProductClass\.id/",$source, $result)){
  32.             $search = $result[0];
  33.             $snipet = "url('productoption_cart_handle_item', {'operation': 'down', 'cartItemId': CartItem.id";
  34.             $replace = $snipet;
  35.             $source = str_replace($search, $replace, $source);
  36.             //debug
  37.             log_info('CartEvent_down 発生');
  38.             $parameters = $event->getParameters();
  39.             $Carts = $parameters['Carts'];
  40.             foreach($Carts as $Cart){
  41.                 foreach($Cart->getCartItems() as $CartItem){
  42.                     $kntn_id = $CartItem->getKntnId();
  43.                     log_info('down kntn_id: '.$kntn_id);
  44.                 }
  45.             }
  46.         }
  47.         if(preg_match("/url\('cart\_handle\_item'\s*,\s*\{'operation'\s*:\s*'up'\s*,\s*'productClassId'\s*:\s*ProductClass\.id/",$source, $result)){
  48.             $search = $result[0];
  49.             $snipet = "url('productoption_cart_handle_item', {'operation': 'up', 'cartItemId': CartItem.id";
  50.             $replace = $snipet;
  51.             $source = str_replace($search, $replace, $source);
  52.             //debug
  53.             log_info('CartEvent_up 発生');
  54.             $parameters = $event->getParameters();
  55.             $Carts = $parameters['Carts'];
  56.             foreach($Carts as $Cart){
  57.                 foreach($Cart->getCartItems() as $CartItem){
  58.                     $kntn_id = $CartItem->getKntnId();
  59.                     log_info('up kntn_id: '.$kntn_id);
  60.                 }
  61.             }
  62.         }
  63.         if(preg_match("/url\('cart\_handle\_item',\s\{'operation'\:\s'remove',\s'productClassId'\:\sProductClass\.id/",$source, $result)){
  64.             $search = $result[0];
  65.             $snipet = "url('productoption_cart_handle_item', {'operation': 'remove', 'cartItemId': CartItem.id";
  66.             $replace = $snipet;
  67.             $source = str_replace($search, $replace, $source);
  68.             //debug
  69.             log_info('CartEvent_remove 発生');
  70.             $parameters = $event->getParameters();
  71.             $Carts = $parameters['Carts'];
  72.             foreach($Carts as $Cart){
  73.                 foreach($Cart->getCartItems() as $CartItem){
  74.                     $kntn_id = $CartItem->getKntnId();
  75.                     log_info('remove kntn_id: '.$kntn_id);
  76.                 }
  77.             }
  78.         }
  79.         if(preg_match("/\<\/div\>[\n|\r\n\\r]\s*<div\sclass\=\"ec\-cartRow\_\_unitPrice\"\>/",$source, $result)){
  80.             $search = $result[0];
  81.             $replace = "{{ include('@ProductOption42/default/Cart/cart_option.twig') }}" . $search;
  82.             $source = str_replace($search, $replace, $source);
  83.         }
  84.         $event->setSource($source);
  85.         $parameters = $event->getParameters();
  86.         $Carts = $parameters['Carts'];
  87.         $isDeliveryFree = $parameters['is_delivery_free'];
  88.         foreach($Carts as $Cart){
  89.             foreach($Cart->getCartItems() as $CartItem){
  90.                 $flg = $CartItem->getDeliveryFreeFlg();
  91.                 if($flg == OptionCategory::ON){
  92.                     if(!$isDeliveryFree[$Cart->getCartKey()]){
  93.                         $isDeliveryFree[$Cart->getCartKey()] = true;
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.         $parameters['is_delivery_free'] = $isDeliveryFree;
  99.         $event->setParameters($parameters);
  100.     }
  101. }