app/Plugin/DeliveryFreeProduct42/Event/DeliveryCoolEvent.php line 40

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\Event\EventArgs;
  13. use Plugin\DeliveryFreeProduct42\Entity\DeliveryFreeConfig;
  14. use Plugin\DeliveryFreeProduct42\Service\UtilService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class DeliveryCoolEvent implements EventSubscriberInterface
  17. {
  18.     /** @var UtilService */
  19.     private $deliveryFreeService;
  20.     /**
  21.      * DeliveryFreeProductEvent constructor.
  22.      * @param UtilService $deliveryFreeService
  23.      */
  24.     public function __construct(UtilService $deliveryFreeService)
  25.     {
  26.         $this->deliveryFreeService $deliveryFreeService;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             'deliverycool.delivery_free.condition' => 'hookDeliveryCoolDeliveryFree',
  32.         ];
  33.     }
  34.     public function hookDeliveryCoolDeliveryFree(EventArgs $event)
  35.     {
  36.         $isFree $event->getArgument('isFree');
  37.         $Shipping $event->getArgument('Shipping');
  38.         $range $this->deliveryFreeService->getConfig(DeliveryFreeConfig::RANGE_NAME);
  39.         $condition $this->deliveryFreeService->getConfig(DeliveryFreeConfig::DEFAULT_NAME);
  40.         if($range == DeliveryFreeConfig::BY_SHIPPING){
  41.             $count 0;
  42.             $total 0;
  43.             foreach($Shipping->getProductOrderItems() as $orderItem){
  44.                 $total++;
  45.                 if($orderItem->getProduct()->getPlgDeliveryfreeproduct() == 1)$count++;
  46.             }
  47.             if($condition == DeliveryFreeConfig::MUST_ALL){
  48.                 if($total == $count)$isFree true;
  49.             }else{
  50.                 if($count 0)$isFree true;
  51.             }
  52.         }elseif($range == DeliveryFreeConfig::BY_ALL){
  53.             $Order $Shipping->getOrder();
  54.             $count 0;
  55.             $total 0;
  56.             /** @var OrderItem $orderDetail */
  57.             foreach($Order->getProductOrderItems() as $orderItem){
  58.                 $total++;
  59.                 if($orderItem->getProduct()->getPlgDeliveryfreeproduct() == 1)$count++;
  60.             }
  61.             if($condition == DeliveryFreeConfig::MUST_ALL){
  62.                 if($total == $count)$isFree true;
  63.             }else{
  64.                 if($count 0)$isFree true;
  65.             }
  66.         }
  67.         $event->setArgument('isFree',$isFree);
  68.     }
  69. }