app/Plugin/CustomerRank42/Event/CartEvent.php line 44

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 Eccube\Service\CartService;
  14. use Plugin\CustomerRank42\Service\CustomerRankService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class CartEvent implements EventSubscriberInterface
  17. {
  18.     private $cartService;
  19.     private $customerRankService;
  20.     public function __construct(
  21.             CartService $cartService,
  22.             CustomerRankService $customerRankService
  23.             )
  24.     {
  25.         $this->cartService $cartService;
  26.         $this->customerRankService $customerRankService;
  27.     }
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             'Cart/index.twig' => 'onTemplateCart',
  35.         ];
  36.     }
  37.     public function onTemplateCart(TemplateEvent $event)
  38.     {
  39.         $parameters $event->getParameters();
  40.         $least $parameters['least'];
  41.         $isDeliveryFree $parameters['is_delivery_free'];
  42.         $CustomerRank $this->customerRankService->getCustomerRank(false);
  43.         if(!is_null($CustomerRank)){
  44.             $Carts $this->cartService->getCarts();
  45.             if (strlen($CustomerRank->getDeliveryFreeCondition()) > 0) {
  46.                 foreach ($Carts as $Cart) {
  47.                     $isDeliveryFree[$Cart->getCartKey()] = false;
  48.                     if ($CustomerRank->getDeliveryFreeCondition() <= $Cart->getTotalPrice()) {
  49.                         $isDeliveryFree[$Cart->getCartKey()] = true;
  50.                     } else {
  51.                         $least[$Cart->getCartKey()] = $CustomerRank->getDeliveryFreeCondition() - $Cart->getTotalPrice();
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.         $parameters['least'] = $least;
  57.         $parameters['is_delivery_free'] = $isDeliveryFree;
  58.         $event->setParameters($parameters);
  59.     }
  60. }