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

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