app/Plugin/DeliveryDate42/Event/ShoppingEvent.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : DeliveryDate4
  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\DeliveryDate42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Event\EccubeEvents;
  14. use Eccube\Event\EventArgs;
  15. use Plugin\DeliveryDate42\Service\ShoppingService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ShoppingEvent implements EventSubscriberInterface
  18. {
  19.     private $entityManager;
  20.     private $shoppingService;
  21.     public function __construct(
  22.             EntityManagerInterface $entityManager,
  23.             ShoppingService $shoppingService
  24.             )
  25.     {
  26.         $this->entityManager $entityManager;
  27.         $this->shoppingService $shoppingService;
  28.     }
  29.     /**
  30.      * @return array
  31.      */
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             EccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE => 'hookFrontShoppingCompleteInitialize',
  36.         ];
  37.     }
  38.     public function hookFrontShoppingCompleteInitialize(EventArgs $event)
  39.     {
  40.         $Order $event->getArgument('Order');
  41.         foreach($Order->getShippings() as $Shipping){
  42.             $shippingDate null;
  43.             /** @var \Eccube\Entity\Shipping $Shipping */
  44.             if(method_exists($Shipping'setShippingScheduleDate')){
  45.                 $shippingDeliveryDate $Shipping->getShippingDeliveryDate();
  46.                 if(!is_null($shippingDeliveryDate))$shipping_date $shippingDeliveryDate->format('Y/m/d');
  47.                 if(isset($shipping_date)){
  48.                     $Delivery $Shipping->getDelivery();
  49.                     $shippingDate = new \DateTime($shipping_date);
  50.                     $dates $this->shoppingService->getDeliveryDates($Delivery$Shipping->getPref());
  51.                     if(!is_null($dates)){
  52.                         $shippingDate->modify('-' $dates 'days');
  53.                     }
  54.                 }else{
  55.                     $minDate $this->shoppingService->getShippingDates($Shipping);
  56.                     if(!is_null($minDate)){
  57.                         $shippingDate $Order->getCreateDate();
  58.                         $shippingDate->modify('+' $minDate 'days');
  59.                     }
  60.                 }
  61.                 if(!is_null($shippingDate)){
  62.                     $Shipping->setShippingScheduleDate($shippingDate);
  63.                     $this->entityManager->persist($Shipping);
  64.                 }
  65.             }
  66.         }
  67.         $this->entityManager->flush();
  68.     }
  69. }