app/Plugin/CustomerRank42/Event/ProductEvent.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : CustomerRank
  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\CustomerRank42\Event;
  12. use Eccube\Event\TemplateEvent;
  13. use Plugin\CustomerRank42\Entity\CustomerRankConfig;
  14. use Plugin\CustomerRank42\Service\CustomerRankService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ProductEvent implements EventSubscriberInterface
  17. {
  18.     private $customerRankService;
  19.     public function __construct(
  20.             CustomerRankService $customerRankService
  21.             )
  22.     {
  23.         $this->customerRankService $customerRankService;
  24.     }
  25.     /**
  26.      * @return array
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             'Product/list.twig' => 'onTemplateProductList',
  32.             'Product/detail.twig' => 'onTemplateProductDetail',
  33.         ];
  34.     }
  35.     public function onTemplateProductDetail(TemplateEvent $event)
  36.     {
  37.         $this->customerRankService->rankCheckForFront();
  38.         $parameters $event->getParameters();
  39.         $Product $parameters['Product'];
  40.         $class_categories = [];
  41.         $loginDisp $this->customerRankService->getConfig('login_disp');
  42.         $initFlag $loginDisp == CustomerRankConfig::DISABLED true false;
  43.         $CustomerRank $this->customerRankService->getCustomerRank($initFlag);
  44.         if(!is_null($CustomerRank)){
  45.             $class_categories[$Product->getId()]['__unselected']['#'] = [
  46.                 'customer_rank_price' => '',
  47.                 'customer_rank_price_inc_tax' => '',
  48.             ];
  49.             foreach($Product->getProductClasses() as $ProductClass){
  50.                 if(!$ProductClass->isVisible())continue;
  51.                 $ClassCategory1 $ProductClass->getClassCategory1();
  52.                 $ClassCategory2 $ProductClass->getClassCategory2();
  53.                 if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  54.                     continue;
  55.                 }
  56.                 $class_category_id1 $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
  57.                 $class_category_id2 $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
  58.                 $class_categories[$Product->getId()][$class_category_id1]['#'] = [
  59.                     'customer_rank_price' => '',
  60.                     'customer_rank_price_inc_tax' => '',
  61.                 ];
  62.                 $class_categories[$Product->getId()][$class_category_id1]['#'.$class_category_id2] = [
  63.                     'customer_rank_price' => number_format($ProductClass->getCustomerRankPrice($CustomerRank->getId())),
  64.                     'customer_rank_price_inc_tax' => number_format($ProductClass->getCustomerRankPriceIncTax($CustomerRank->getId())),
  65.                 ];
  66.             }
  67.         }
  68.         $parameters['CustomerRank'] = $CustomerRank;
  69.         $parameters['CustomerPrices'] = $class_categories;
  70.         $event->setParameters($parameters);
  71.         $twig '@CustomerRank42/default/Product/product_js.twig';
  72.         $event->addSnippet($twig);
  73.     }
  74.     public function onTemplateProductList(TemplateEvent $event)
  75.     {
  76.         $this->customerRankService->rankCheckForFront();
  77.         $parameters $event->getParameters();
  78.         $pagination $parameters['pagination'];
  79.         $class_categories = [];
  80.         $loginDisp $this->customerRankService->getConfig('login_disp');
  81.         $initFlag $loginDisp == CustomerRankConfig::DISABLED true false;
  82.         $CustomerRank $this->customerRankService->getCustomerRank($initFlag);
  83.         if(!is_null($CustomerRank)){
  84.             foreach($pagination as $Product){
  85.                 $class_categories[$Product->getId()]['__unselected']['#'] = [
  86.                     'customer_rank_price' => '',
  87.                     'customer_rank_price_inc_tax' => '',
  88.                 ];
  89.                 foreach($Product->getProductClasses() as $ProductClass){
  90.                     if(!$ProductClass->isVisible())continue;
  91.                     $ClassCategory1 $ProductClass->getClassCategory1();
  92.                     $ClassCategory2 $ProductClass->getClassCategory2();
  93.                     if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  94.                         continue;
  95.                     }
  96.                     $class_category_id1 $ClassCategory1 ? (string) $ClassCategory1->getId() : '__unselected2';
  97.                     $class_category_id2 $ClassCategory2 ? (string) $ClassCategory2->getId() : '';
  98.                     $class_categories[$Product->getId()][$class_category_id1]['#'] = [
  99.                         'customer_rank_price' => '',
  100.                         'customer_rank_price_inc_tax' => '',
  101.                     ];
  102.                     $class_categories[$Product->getId()][$class_category_id1]['#'.$class_category_id2] = [
  103.                         'customer_rank_price' => number_format($ProductClass->getCustomerRankPrice($CustomerRank->getId())),
  104.                         'customer_rank_price_inc_tax' => number_format($ProductClass->getCustomerRankPriceIncTax($CustomerRank->getId())),
  105.                     ];
  106.                 }
  107.             }
  108.         }
  109.         $parameters['CustomerRank'] = $CustomerRank;
  110.         $parameters['CustomerPrices'] = $class_categories;
  111.         $event->setParameters($parameters);
  112.         $twig '@CustomerRank42/default/Product/product_js.twig';
  113.         $event->addSnippet($twig);
  114.     }
  115. }