app/Plugin/ProductOption42/Event/ShoppingEvent.php line 142

Open in your IDE?
  1. <?php
  2. /*
  3. * Plugin Name : ProductOption
  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\ProductOption42\Event;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Entity\Master\OrderItemType;
  14. use Eccube\Entity\OrderItem;
  15. use Eccube\Entity\Shipping;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Event\TemplateEvent;
  19. use Eccube\Form\Type\ShippingMultipleType;
  20. use Eccube\Repository\Master\OrderItemTypeRepository;
  21. use Plugin\ProductOption42\Entity\OrderItemOption;
  22. use Plugin\ProductOption42\Entity\OrderItemOptionCategory;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  25. class ShoppingEvent implements EventSubscriberInterface
  26. {
  27.     private $entityManager;
  28.     private $orderItemTypeRepository;
  29.     public function __construct(
  30.             EntityManagerInterface $entityManager,
  31.             OrderItemTypeRepository $orderItemTypeRepository
  32.             )
  33.     {
  34.         $this->entityManager $entityManager;
  35.         $this->orderItemTypeRepository $orderItemTypeRepository;
  36.     }
  37.     /**
  38.      * @return array
  39.      */
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             'Shopping/index.twig' => 'onTempleteShoppingIndex',
  44.             'Shopping/confirm.twig' => 'onTempleteShoppingIndex',
  45.             'Shopping/shipping_multiple.twig' => 'onTempleteShoppingShippingMultiple',
  46.             EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_INITIALIZE => 'onHookShoppingShippingMultipleInitialize',
  47.             EccubeEvents::FRONT_SHOPPING_SHIPPING_MULTIPLE_COMPLETE => 'onHookShoppingShippingMultipleComplete',
  48.         ];
  49.     }
  50.     public function onTempleteShoppingIndex(TemplateEvent $event)
  51.     {
  52.         $source $event->getSource();
  53.         if(preg_match("/\<p\>\{\{\sorderItem\.priceIncTax\|price\s\}\}/",$source$result)){
  54.             $search $result[0];
  55.             $replace "{{ include('@ProductOption42/default/Shopping/orderitem_option.twig') }}" $search;
  56.             $source str_replace($search$replace$source);
  57.         }
  58.         $event->setSource($source);
  59.     }
  60.     public function onTempleteShoppingShippingMultiple(TemplateEvent $event)
  61.     {
  62.         $source $event->getSource();
  63.         if(preg_match("/\{\%\sif\sorderItem\.productClass\.id\s\=\=\skey\s\%\}/",$source$result)){
  64.             $search $result[0];
  65.             $replace "{% if orderItem.productClass.id ~ '_' ~ orderItem.option_serial == key %}";
  66.             $source str_replace($search$replace$source);
  67.         }
  68.         if(preg_match("/\<div\sclass\=\"ec\-AddAddress\_\_itemtPrice\"\>/",$source$result)){
  69.             $search $result[0];
  70.             $replace $search "{{ include('@ProductOption42/default/Shopping/shipping_multiple_option.twig') }}";
  71.             $source str_replace($search$replace$source);
  72.         }
  73.         $event->setSource($source);
  74.         $parameters $event->getParameters();
  75.         $OrderItems $parameters['OrderItems'];
  76.         $OrderItemsForFormBuilder = [];
  77.         $ItemQuantitiesByOption = [];
  78.         $tmpIds = [];
  79.         $Order $OrderItems[0]->getOrder();
  80.         foreach($Order->getProductOrderItems() as $OrderItem){
  81.             $quantity $OrderItem->getQuantity();
  82.             $itemId $OrderItem->getProductClass()->getId();
  83.             $serial $OrderItem->getOptionSerial();
  84.             $key $itemId .'_'$serial;
  85.             if (!in_array($key$tmpIds)) {
  86.                 $OrderItemsForFormBuilder[] = $OrderItem;
  87.                 $tmpIds[] = $key;
  88.                 $ItemQuantitiesByOption[$key] = $quantity;
  89.             }else{
  90.                 $ItemQuantitiesByOption[$key] += $quantity;
  91.             }
  92.         }
  93.         $parameters['OrderItems'] = $OrderItemsForFormBuilder;
  94.         $parameters['compItemQuantities'] = $ItemQuantitiesByOption;
  95.         $event->setParameters($parameters);
  96.     }
  97.     public function onHookShoppingShippingMultipleInitialize(EventArgs $event)
  98.     {
  99.         $builder $event->getArgument('builder');
  100.         $Order $event->getArgument('Order');
  101.         $OrderItemsForFormBuilder = [];
  102.         $tmpIds = [];
  103.         foreach($Order->getProductOrderItems() as $OrderItem){
  104.             $itemId $OrderItem->getProductClass()->getId();
  105.             $serial $OrderItem->getOptionSerial();
  106.             $key $itemId .'_'$serial;
  107.             if (!in_array($key$tmpIds)) {
  108.                 $OrderItemsForFormBuilder[] = $OrderItem;
  109.                 $tmpIds[] = $key;
  110.             }
  111.         }
  112.         $builder->remove('shipping_multiple');
  113.         $builder
  114.             ->add('shipping_multiple'CollectionType::class, [
  115.                 'entry_type' => ShippingMultipleType::class,
  116.                 'data' => $OrderItemsForFormBuilder,
  117.                 'allow_add' => true,
  118.                 'allow_delete' => true,
  119.             ]);
  120.     }
  121.     public function onHookShoppingShippingMultipleComplete(EventArgs $event)
  122.     {
  123.         $form $event->getArgument('form');
  124.         $Order $event->getArgument('Order');
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             $data $form['shipping_multiple'];
  127.             // フォームの入力から、送り先ごとに商品の数量を集計する
  128.             $arrOrderItemTemp = [];
  129.             foreach ($data as $multiples) {
  130.                 $OrderItem $multiples->getData();
  131.                 foreach ($multiples as $items) {
  132.                     foreach ($items as $item) {
  133.                         $CustomerAddress $item['customer_address']->getData();
  134.                         $customerAddressName $CustomerAddress->getShippingMultipleDefaultName();
  135.                         $itemId $OrderItem->getProductClass()->getId() . '_' $OrderItem->getOptionSerial();
  136.                         $quantity $item['quantity']->getData();
  137.                         if (isset($arrOrderItemTemp[$customerAddressName]) && array_key_exists($itemId$arrOrderItemTemp[$customerAddressName])) {
  138.                             $arrOrderItemTemp[$customerAddressName][$itemId] = $arrOrderItemTemp[$customerAddressName][$itemId] + $quantity;
  139.                         } else {
  140.                             $arrOrderItemTemp[$customerAddressName][$itemId] = $quantity;
  141.                         }
  142.                     }
  143.                 }
  144.             }
  145.             // -- ここから先がお届け先を再生成する処理 --
  146.             // お届け先情報をすべて削除
  147.             /** @var Shipping $Shipping */
  148.             foreach ($Order->getShippings() as $Shipping) {
  149.                 foreach ($Shipping->getOrderItems() as $OrderItem) {
  150.                     $Shipping->removeOrderItem($OrderItem);
  151.                     $Order->removeOrderItem($OrderItem);
  152.                     $this->entityManager->remove($OrderItem);
  153.                 }
  154.                 $Order->removeShipping($Shipping);
  155.                 $this->entityManager->remove($Shipping);
  156.             }
  157.             // お届け先のリストを作成する
  158.             $ShippingList = [];
  159.             foreach ($data as $multiples) {
  160.                 $OrderItem $multiples->getData();
  161.                 $ProductClass $OrderItem->getProductClass();
  162.                 $Delivery $OrderItem->getShipping()->getDelivery();
  163.                 $saleTypeId $ProductClass->getSaleType()->getId();
  164.                 foreach ($multiples as $items) {
  165.                     foreach ($items as $item) {
  166.                         $CustomerAddress $item['customer_address']->getData();
  167.                         $customerAddressName $CustomerAddress->getShippingMultipleDefaultName();
  168.                         if (isset($ShippingList[$customerAddressName][$saleTypeId])) {
  169.                             continue;
  170.                         }
  171.                         $Shipping = new Shipping();
  172.                         $Shipping
  173.                             ->setOrder($Order)
  174.                             ->setFromCustomerAddress($CustomerAddress)
  175.                             ->setDelivery($Delivery);
  176.                         $Order->addShipping($Shipping);
  177.                         $ShippingList[$customerAddressName][$saleTypeId] = $Shipping;
  178.                     }
  179.                 }
  180.             }
  181.             // お届け先のリストを保存
  182.             foreach ($ShippingList as $ShippingListByAddress) {
  183.                 foreach ($ShippingListByAddress as $Shipping) {
  184.                     $this->entityManager->persist($Shipping);
  185.                 }
  186.             }
  187.             $ProductOrderType $this->orderItemTypeRepository->find(OrderItemType::PRODUCT);
  188.             // お届け先に、配送商品の情報(OrderItem)を関連付ける
  189.             foreach ($data as $multiples) {
  190.                 /** @var OrderItem $OrderItem */
  191.                 $OrderItem $multiples->getData();
  192.                 $ProductClass $OrderItem->getProductClass();
  193.                 $Product $OrderItem->getProduct();
  194.                 $saleTypeId $ProductClass->getSaleType()->getId();
  195.                 $itemId $OrderItem->getProductClass()->getId() . '_' $OrderItem->getOptionSerial();
  196.                 $optionSerial $OrderItem->getOptionSerial();
  197.                 $OrderItemOptions $OrderItem->getOrderItemOptions();
  198.                 $optionPrice $OrderItem->getOptionPrice();
  199.                 $optionTax $OrderItem->getOptionTax();
  200.                 foreach ($multiples as $items) {
  201.                     foreach ($items as $item) {
  202.                         $CustomerAddress $item['customer_address']->getData();
  203.                         $customerAddressName $CustomerAddress->getShippingMultipleDefaultName();
  204.                         // お届け先から商品の数量を取得
  205.                         $quantity 0;
  206.                         if (isset($arrOrderItemTemp[$customerAddressName]) && array_key_exists($itemId$arrOrderItemTemp[$customerAddressName])) {
  207.                             $quantity $arrOrderItemTemp[$customerAddressName][$itemId];
  208.                             unset($arrOrderItemTemp[$customerAddressName][$itemId]);
  209.                         } else {
  210.                             // この配送先には送る商品がないのでスキップ(通常ありえない)
  211.                             continue;
  212.                         }
  213.                         // 関連付けるお届け先のインスタンスを取得
  214.                         $Shipping $ShippingList[$customerAddressName][$saleTypeId];
  215.                         // インスタンスを生成して保存
  216.                         $OrderItem = new OrderItem();
  217.                         $OrderItem->setShipping($Shipping)
  218.                             ->setOrder($Order)
  219.                             ->setProductClass($ProductClass)
  220.                             ->setProduct($Product)
  221.                             ->setProductName($Product->getName())
  222.                             ->setProductCode($ProductClass->getCode())
  223.                             ->setPrice($ProductClass->getPrice02())
  224.                             ->setQuantity($quantity)
  225.                             ->setOrderItemType($ProductOrderType)
  226.                             ->setOptionSerial($optionSerial);
  227.                         $ClassCategory1 $ProductClass->getClassCategory1();
  228.                         if (!is_null($ClassCategory1)) {
  229.                             $OrderItem->setClasscategoryName1($ClassCategory1->getName());
  230.                             $OrderItem->setClassName1($ClassCategory1->getClassName()->getName());
  231.                         }
  232.                         $ClassCategory2 $ProductClass->getClassCategory2();
  233.                         if (!is_null($ClassCategory2)) {
  234.                             $OrderItem->setClasscategoryName2($ClassCategory2->getName());
  235.                             $OrderItem->setClassName2($ClassCategory2->getClassName()->getName());
  236.                         }
  237.                         foreach($OrderItemOptions as $OrderItemOption){
  238.                             $newOrderItemOption = new OrderItemOption();
  239.                             $newOrderItemOption->setOptionId($OrderItemOption->getOptionId())
  240.                                                ->setOrderItem($OrderItem)
  241.                                                ->setLabel($OrderItemOption->getLabel())
  242.                                                ->setSortNo($OrderItemOption->getSortNo());
  243.                             foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategory){
  244.                                 $newOrderItemOptionCategory = new OrderItemOptionCategory();
  245.                                 $newOrderItemOptionCategory->setOptionCategoryId($OrderItemOptionCategory->getOptionCategoryId())
  246.                                                            ->setOrderItemOption($newOrderItemOption)
  247.                                                            ->setValue($OrderItemOptionCategory->getValue())
  248.                                                            ->setPrice($OrderItemOptionCategory->getPrice())
  249.                                                            ->setTax($OrderItemOptionCategory->getTax())
  250.                                                            ->setDeliveryFreeFlg($OrderItemOptionCategory->getDeliveryFreeFlg())
  251.                                                            ->setSortNo($OrderItemOptionCategory->getSortNo());
  252.                                 $newOrderItemOption->addOrderItemOptionCategory($newOrderItemOptionCategory);
  253.                             }
  254.                             $OrderItem->addOrderItemOption($newOrderItemOption);
  255.                         }
  256.                         $OrderItem->setPrice($OrderItem->getPrice() + $optionPrice);
  257.                         $OrderItem->setTax($OrderItem->getTax() + $optionTax);
  258.                         $OrderItem->setOptionSetFlg(true);
  259.                         $Shipping->addOrderItem($OrderItem);
  260.                         $Order->addOrderItem($OrderItem);
  261.                         $this->entityManager->persist($OrderItem);
  262.                     }
  263.                 }
  264.             }
  265.             $this->entityManager->flush();
  266.         }
  267.     }
  268. }