app/Plugin/DeliveryFreeProduct42/Event/CartEvent.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : DeliveryFreeProduct
  4. *
  5. * Copyright (C) 2016 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\DeliveryFreeProduct42\Event;
  12. use Eccube\Entity\Cart;
  13. use Eccube\Entity\CartItem;
  14. use Eccube\Event\TemplateEvent;
  15. use Plugin\DeliveryFreeProduct42\Entity\DeliveryFreeConfig;
  16. use Plugin\DeliveryFreeProduct42\Service\UtilService;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class CartEvent implements EventSubscriberInterface
  19. {
  20.     /** @var UtilService */
  21.     private $deliveryFreeService;
  22.     /**
  23.      * DeliveryFreeProductEvent constructor.
  24.      * @param UtilService $deliveryFreeService
  25.      */
  26.     public function __construct(UtilService $deliveryFreeService)
  27.     {
  28.         $this->deliveryFreeService $deliveryFreeService;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             'Cart/index.twig' => 'onRenderCart',
  34.         ];
  35.     }
  36.     public function onRenderCart(TemplateEvent $event)
  37.     {
  38.         $parameters $event->getParameters();
  39.         /** @var Cart[] $Carts */
  40.         $Carts $parameters['Carts'];
  41.         $arrDeliveryFree = [];
  42.         $originalDeliveryFree $parameters['is_delivery_free'];
  43.         $condition $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
  44.         $needAddTemplate false;
  45.         $isCartFree = [];
  46.         foreach ($Carts as $key => $Cart) {
  47.             // default
  48.             $arrDeliveryFree[$Cart->getCartKey()] = $originalDeliveryFree[$Cart->getCartKey()];
  49.             if(!$originalDeliveryFree[$Cart->getCartKey()]) {
  50.                 $count 0;
  51.                 $total 0;
  52.                 /** @var CartItem $cart_item */
  53.                 foreach ($Cart->getCartItems() as $cart_item) {
  54.                     if (!$cart_item->isProduct()) {
  55.                         continue;
  56.                     }
  57.                     $total++;
  58.                     if($cart_item->getProductClass()->getProduct()->getPlgDeliveryfreeproduct() == 1)$count++;
  59.                 }
  60.                 $deliv_free_flg false;
  61.                 if ($condition == DeliveryFreeConfig::MUST_ALL) {
  62.                     if ($total == $count) {
  63.                         $deliv_free_flg true;
  64.                         $needAddTemplate true;
  65.                     }
  66.                 } else {
  67.                     if ($count 0) {
  68.                         $deliv_free_flg true;
  69.                         $needAddTemplate true;
  70.                     }
  71.                 }
  72.                 $arrDeliveryFree[$Cart->getCartKey()] = $deliv_free_flg;
  73.                 $isCartFree[$key] = $deliv_free_flg;
  74.             }
  75.         }
  76.         $parameters['is_delivery_free'] = $arrDeliveryFree;
  77.         $parameters['is_cart_free'] = $isCartFree;
  78.         $event->setParameters($parameters);
  79.         if ($needAddTemplate) {
  80.             $event->addSnippet("@DeliveryFreeProduct42/default/Cart/delivery_free_message.twig");
  81.         }
  82.     }
  83. }