app/Plugin/FlashSale/Service/Event/ProductClassRuleEventSubscriber.php line 97

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Flash Sale plugin
  4.  *
  5.  * Copyright(c) ECCUBE VN LAB. All Rights Reserved.
  6.  *
  7.  * https://www.facebook.com/groups/eccube.vn
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\FlashSale\Service\Event;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Entity\Product;
  16. use Eccube\Entity\ProductClass;
  17. use Eccube\Common\EccubeConfig;
  18. class ProductClassRuleEventSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var EccubeConfig
  22.      */
  23.     protected $eccubeConfig;
  24.     /**
  25.      * @var \NumberFormatter
  26.      */
  27.     protected $formatter;
  28.     /**
  29.      * ProductClassRuleEventSubscriber constructor.
  30.      *
  31.      * @param EccubeConfig $eccubeConfig
  32.      */
  33.     public function __construct(
  34.         EccubeConfig $eccubeConfig
  35.     ) {
  36.         $this->eccubeConfig $eccubeConfig;
  37.         $this->formatter = new \NumberFormatter($this->eccubeConfig['locale'], \NumberFormatter::CURRENCY);
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      *
  42.      * @return array
  43.      */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             'Product/detail.twig' => 'onTemplateProductDetail',
  48.             'Product/list.twig' => 'onTemplateProductList',
  49.         ];
  50.     }
  51.     /**
  52.      * Change price of product depend on flash sale
  53.      *
  54.      * @param TemplateEvent $event
  55.      */
  56.     public function onTemplateProductDetail(TemplateEvent $event)
  57.     {
  58.         if (!$event->hasParameter('Product')) {
  59.             return;
  60.         }
  61.         $json = [];
  62.         /** @var ProductClass $ProductClass */
  63.         foreach ($event->getParameter('Product')->getProductClasses() as $ProductClass) {
  64.             $discountValue $ProductClass->getFlashSaleDiscount();
  65.             if ($discountValue) {
  66.                 $discountPrice $ProductClass->getFlashSaleDiscountPrice();
  67.                 $discountPercent $ProductClass->getFlashSaleDiscountPercent();
  68.                 $json[$ProductClass->getId()] = [
  69.                     'message' => '<p class="ec-color-red"><span>'.$this->formatter->formatCurrency($discountPrice$this->eccubeConfig['currency']).'</span> (-'.$discountPercent.'%)</p>',
  70.                 ];
  71.             }
  72.         }
  73.         if (empty($json)) {
  74.             return;
  75.         }
  76.         $event->setParameter('ProductFlashSale'json_encode($json));
  77.         $event->addSnippet('@FlashSale/default/detail.twig');
  78.     }
  79.     /**
  80.      * Change price of product depend on flash sale
  81.      *
  82.      * @param TemplateEvent $event
  83.      */
  84.     public function onTemplateProductList(TemplateEvent $event)
  85.     {
  86.         if (!$event->hasParameter('pagination')) {
  87.             return;
  88.         }
  89.         $json = [];
  90.         /** @var Product $Product */
  91.         foreach ($event->getParameter('pagination') as $Product) {
  92.             /** @var ProductClass $ProductClass */
  93.             foreach ($Product->getProductClasses() as $ProductClass) {
  94.                 $discountValue $ProductClass->getFlashSaleDiscount();
  95.                 if ($discountValue) {
  96.                     $discountPrice $ProductClass->getFlashSaleDiscountPrice();
  97.                     $discountPercent $ProductClass->getFlashSaleDiscountPercent();
  98.                     $json[$ProductClass->getId()] = [
  99.                         'message' => '<p class="ec-color-red"><span>'.$this->formatter->formatCurrency($discountPrice$this->eccubeConfig['currency']).'</span> (-'.$discountPercent.'%)</p>',
  100.                     ];
  101.                 }
  102.             }
  103.         }
  104.         if (empty($json)) {
  105.             return;
  106.         }
  107.         $event->setParameter('ProductFlashSale'json_encode($json));
  108.         $event->addSnippet('@FlashSale/default/list.twig');
  109.     }
  110. }