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

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