app/Plugin/CustomerPlus42/Event/ShippingMultipleEvent.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : CustomerPlus4
  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\CustomerPlus42\Event;
  12. use Eccube\Event\EccubeEvents;
  13. use Eccube\Event\EventArgs;
  14. use Eccube\Service\OrderHelper;
  15. use Plugin\CustomerPlus42\Entity\CustomerAddressCustom;
  16. use Plugin\CustomerPlus42\Entity\CustomerData;
  17. use Plugin\CustomerPlus42\Entity\CustomerDataDetail;
  18. use Plugin\CustomerPlus42\Entity\CustomerItem;
  19. use Plugin\CustomerPlus42\Event\AbstractEvent;
  20. use Symfony\Component\DependencyInjection\ContainerInterface;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  24. class ShippingMultipleEvent extends AbstractEvent implements EventSubscriberInterface
  25. {
  26.     private $session;
  27.     private $authorizationChecker;
  28.     private $orderHelper;
  29.     public function __construct(
  30.             ContainerInterface $container,
  31.             SessionInterface $session,
  32.             AuthorizationCheckerInterface $authorizationChecker,
  33.             OrderHelper $orderHelper
  34.             )
  35.     {
  36.         parent::__construct($container);
  37.         $this->session $session;
  38.         $this->authorizationChecker $authorizationChecker;
  39.         $this->orderHelper $orderHelper;
  40.     }
  41.     /**
  42.      * @return array
  43.      */
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_INITIALIZE => 'hookFrontShoppingShippingMultipleInitialize',
  48.             EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_COMPLETE => ['hookFrontShoppingShippingMultipleComplete',-10],
  49.         ];
  50.     }
  51.     public function hookFrontShoppingShippingMultipleInitialize(EventArgs $event)
  52.     {
  53.         //ゲスト購入時の場合は削除前のShipping情報のカスタム要素を保持
  54.         $Order $event->getArgument('Order');
  55.         $Shippings $Order->getShippings();
  56.         $ShippingCustoms = [];
  57.         $ShippingNames = [];
  58.         $CustomerDatas = [];
  59.         $entityManager $this->container->get('doctrine.orm.entity_manager');
  60.         $customerItemRepository $entityManager->getRepository('Plugin\CustomerPlus42\Entity\CustomerItem');
  61.         foreach($Shippings as $Shipping){
  62.             $CustomerDatas = [];
  63.             foreach($Shipping->getShippingCustoms() as $ShippingCustom){
  64.                 $CustomerItem $customerItemRepository->find($ShippingCustom->getCustomerItemId());
  65.                 foreach($ShippingCustom->getCustomerData()->getDetails() as $Detail){
  66.                     $CustomerDatas[$ShippingCustom->getCustomerItemId()][] = ($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE) ? $Detail->getNumValue() : $Detail->getValue();
  67.                 }
  68.             }
  69.             $ShippingCustoms[$Shipping->getId()] = $CustomerDatas;
  70.             $ShippingNames[$Shipping->getId()] = $Shipping->getShippingMultipleDefaultName();
  71.         }
  72.         $this->session->set('eccube.front.shopping.nonmember.shipping'serialize($ShippingNames));
  73.         $this->session->set('eccube.front.shopping.nonmember.shippingCustom'serialize($ShippingCustoms));
  74.     }
  75.     public function hookFrontShoppingShippingMultipleComplete(EventArgs $event)
  76.     {
  77.         $form $event->getArgument('form');
  78.         $Order $event->getArgument('Order');
  79.         $data $form['shipping_multiple'];
  80.         foreach ($data as $multiples) {
  81.             foreach ($multiples as $items) {
  82.                 foreach ($items as $item) {
  83.                     $CustomerAddress $item['customer_address']->getData();
  84.                     $customerAddressName $CustomerAddress->getShippingMultipleDefaultName();
  85.                     if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  86.                         foreach($Order->getShippings() as $Shipping){
  87.                             $shippingName $Shipping->getShippingMultipleDefaultName();
  88.                             if($customerAddressName == $shippingName){
  89.                                 if($customerAddressName == $Order->getShippingMultipleDefaultName()){
  90.                                     foreach($Order->getOrderCustoms() as $OrderCustom){
  91.                                         $CustomerAddressCustom = new CustomerAddressCustom();
  92.                                         $CustomerAddressCustom->setCustomerData($OrderCustom->getCustomerData())
  93.                                                           ->setCustomerItemId($OrderCustom->getCustomerItemId())
  94.                                                           ->setCustomerAddress($CustomerAddress);
  95.                                         $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  96.                                     }
  97.                                 }
  98.                                 $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  99.                             }
  100.                         }
  101.                     }else{
  102.                         //ゲスト購入の場合は削除前に保持されていたShipping情報からデータを復元
  103.                         if ($NonMember $this->orderHelper->getNonMember()) {
  104.                             $entityManager $this->container->get('doctrine.orm.entity_manager');
  105.                             $customerItemRepository $entityManager->getRepository('Plugin\CustomerPlus42\Entity\CustomerItem');
  106.                             if ($MemShippings $this->session->get('eccube.front.shopping.nonmember.shipping')) {
  107.                                 $MemShippingNames unserialize($MemShippings);
  108.                                 $ShippingCustoms $this->session->get('eccube.front.shopping.nonmember.shippingCustom');
  109.                                 $ShippingCustoms unserialize($ShippingCustoms);
  110.                                 $flg false;
  111.                                 foreach($Order->getShippings() as $Shipping){
  112.                                     $shippingName $Shipping->getShippingMultipleDefaultName();
  113.                                     if($customerAddressName == $shippingName){
  114.                                         foreach($MemShippingNames as $memShippingId => $memShippingName){
  115.                                             if($customerAddressName == $memShippingName){
  116.                                                 foreach($ShippingCustoms as $shipping_id => $ShippingCustom){
  117.                                                     if($shipping_id == $memShippingId){
  118.                                                         foreach($ShippingCustom as $customer_item_id => $Details){
  119.                                                             $CustomerData = new CustomerData();
  120.                                                             $CustomerItem $customerItemRepository->find($customer_item_id);
  121.                                                             $CustomerData->setCustomerItem($CustomerItem);
  122.                                                             foreach($Details as $value){
  123.                                                                 $detail = new CustomerDataDetail();
  124.                                                                 $detail->setCustomerData($CustomerData);
  125.                                                                 $disp_value $value;
  126.                                                                 if($CustomerItem->getInputType() == CustomerItem::DATE_TYPE){
  127.                                                                     if(!is_null($value)){
  128.                                                                         $value = new \DateTime($value);
  129.                                                                     }
  130.                                                                     $detail->setDateValue($value);
  131.                                                                 }
  132.                                                                 if($CustomerItem->getInputType() >= CustomerItem::SELECT_TYPE){
  133.                                                                     $detail->setNumValue(intval($value));
  134.                                                                     foreach($CustomerItem->getOptions() as $Option){
  135.                                                                         if($value == $Option->getId())$disp_value $Option->getText();
  136.                                                                     }
  137.                                                                 }
  138.                                                                 $detail->setValue($disp_value);
  139.                                                                 $CustomerData->addDetail($detail);
  140.                                                             }
  141.                                                             $CustomerAddressCustom = new CustomerAddressCustom();
  142.                                                             $CustomerAddressCustom->setCustomerData($CustomerData)
  143.                                                                     ->setCustomerItemId($customer_item_id)
  144.                                                                     ->setCustomerAddress($CustomerAddress);
  145.                                                             $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  146.                                                         }
  147.                                                     }
  148.                                                 }
  149.                                                 $flg true;
  150.                                                 break;
  151.                                             }
  152.                                         }
  153.                                         $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  154.                                     }
  155.                                 }
  156.                                 if(!$flg && $customerAddressName == $Order->getShippingMultipleDefaultName()){
  157.                                     foreach($Order->getOrderCustoms() as $OrderCustom){
  158.                                         $CustomerAddressCustom = new CustomerAddressCustom();
  159.                                         $CustomerAddressCustom->setCustomerData($OrderCustom->getCustomerData())
  160.                                                 ->setCustomerItemId($OrderCustom->getCustomerItemId())
  161.                                                 ->setCustomerAddress($CustomerAddress);
  162.                                         $CustomerAddress->addCustomerAddressCustom($CustomerAddressCustom);
  163.                                     }
  164.                                     $this->setShippingFromCustomerAddress($Shipping,$CustomerAddress);
  165.                                 }
  166.                             }
  167.                         }
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.     }
  173. }