app/Plugin/ProductOption42/Service/PurchaseFlow/Processor/OptionPreprocessor.php line 62

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\Service\PurchaseFlow\Processor;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Eccube\Annotation\CartFlow;
  14. use Eccube\Annotation\ShoppingFlow;
  15. use Eccube\Entity\ItemInterface;
  16. use Eccube\Entity\OrderItem;
  17. use Eccube\Entity\Plugin;
  18. use Eccube\Service\CartService;
  19. use Eccube\Service\PurchaseFlow\ItemPreprocessor;
  20. use Eccube\Service\PurchaseFlow\PurchaseContext;
  21. use Eccube\Service\TaxRuleService;
  22. use Plugin\ProductOption42\Entity\Option;
  23. use Plugin\ProductOption42\Entity\OptionCategory;
  24. use Plugin\ProductOption42\Entity\OrderItemOption;
  25. use Plugin\ProductOption42\Entity\OrderItemOptionCategory;
  26. use Plugin\ProductOption42\Util\CommonUtil;
  27. /**
  28.  * @CartFlow
  29.  * @ShoppingFlow
  30.  */
  31. class OptionPreprocessor implements ItemPreprocessor
  32. {
  33.     private $entityManager;
  34.     private $cartService;
  35.     private $taxRuleService;
  36.     public function __construct(
  37.             EntityManagerInterface $entityManager,
  38.             CartService $cartService,
  39.             TaxRuleService $taxRuleService
  40.             )
  41.     {
  42.         $this->entityManager $entityManager;
  43.         $this->cartService $cartService;
  44.         $this->taxRuleService $taxRuleService;
  45.     }
  46.     /**
  47.      * @param ItemInterface $item
  48.      * @param PurchaseContext $context
  49.      */
  50.     public function process(ItemInterface $itemPurchaseContext $context)
  51.     {
  52.         if (!$item->isProduct()) {
  53.             return;
  54.         }
  55. dump('OptionPreprocessor');
  56.         $Plugin $this->entityManager->getRepository(Plugin::class)->findOneBy(['code' => 'CustomerRank42']);
  57. dump($Plugin);
  58.         if ($item instanceof OrderItem) {
  59. dump(strlen($item->getOptionSerial()));
  60.             if(strlen($item->getOptionSerial()) == 0){
  61. dump($item);
  62.                 $optionRepository $this->entityManager->getRepository(Option::class);
  63.                 $optionCategoryRepository $this->entityManager->getRepository(OptionCategory::class);
  64.                 foreach($this->cartService->getCarts() as $Cart){
  65.                     $CartItems $Cart->getCartItems();
  66.                     foreach($CartItems as $CartItem){
  67. dump($CartItem);
  68. dump($item);
  69.                         $ProductClass1 $item->getProductClass();
  70.                         $ProductClass2 $CartItem->getProductClass();
  71.                         $product_class_id1 $ProductClass1 ? (string) $ProductClass1->getId() : null;
  72.                         $product_class_id2 $ProductClass2 ? (string) $ProductClass2->getId() : null;
  73.                         if ($product_class_id1 === $product_class_id2 && $item->getQuantity() === $CartItem->getQuantity()) {
  74.                             foreach($item->getOrder()->getProductOrderItems() as $orderItem){
  75.                                 if(strlen($orderItem->getOptionSerial()) == 0)continue;
  76.                                 $ProductClass $orderItem->getProductClass();
  77.                                 $product_class_id $ProductClass ? (string) $ProductClass->getId() : null;
  78.                                 if($product_class_id1 === $product_class_id && CommonUtil::compareArray(json_decode($orderItem->getOptionSerial(),true),json_decode($CartItem->getOptionSerial(),true))){
  79.                                     $item->setOptionSerial($CartItem->getOptionSerial());
  80.                                     continue 2;
  81.                                 }
  82.                             }
  83.                             $optionPrice 0;
  84.                             $optionTax 0;
  85.                             $sortNo 0;
  86.                             if(!is_array($CartItem->getArrOption()))continue;
  87.                             foreach($CartItem->getArrOption() as $optionId => $values){
  88.                                 if(!is_array($values))$values = [$values];
  89.                                 $Option $optionRepository->find($optionId);
  90.                                 if(is_null($Option))continue;
  91.                                 $OrderItemOption = new OrderItemOption();
  92.                                 $OrderItemOption->setLabel($Option->getName())
  93.                                                 ->setOptionId($Option->getId())
  94.                                                 ->setOrderItem($item)
  95.                                                 ->setSortNo($sortNo++);
  96.                                 $categorySortNo 0;
  97.                                 foreach($values as $value){
  98.                                     dump($value);
  99.                                     if(
  100.                                         $Option->getType() == Option::SELECT_TYPE
  101.                                         || $Option->getType() == Option::RADIO_TYPE
  102.                                         || $Option->getType() == Option::CHECKBOX_TYPE
  103.                                       ){
  104.                                         $OptionCategory $optionCategoryRepository->find($value);
  105.                                         if(is_null($OptionCategory))continue;
  106.                                         $value $OptionCategory->getName();
  107.                                     }else{
  108.                                         $OptionCategory null;
  109.                                         $OptionCategories $Option->getOptionCategories();
  110.                                         if(count($OptionCategories) > 0)$OptionCategory $OptionCategories[0];
  111.                                     }
  112.                                     
  113.                                     //dump($OptionCategory);
  114.                                     
  115.                                     $OrderItemOptionCategory = new OrderItemOptionCategory();
  116.                                     $OrderItemOptionCategory
  117.                                                             ->setOrderItemOption($OrderItemOption)
  118.                                                             ->setSortNo($categorySortNo++)
  119.                                                             ->setValue($value);
  120.                                     if(!is_null($OptionCategory)){
  121.                                         $option_price $OptionCategory->getValue();
  122.                                         if($Option->getType() == Option::NUMBER_TYPE){
  123.                                             if($OptionCategory->getMultipleFlg())$option_price *= $value;
  124.                                         }
  125.                                         $OrderItemOptionCategory->setDeliveryFreeFlg($OptionCategory->getDeliveryFreeFlg())
  126.                                                                 ->setOptionCategoryId($OptionCategory->getId())
  127.                                                                 ->setPrice($option_price)
  128.                                                                 ->setTax($this->taxRuleService->getTax($option_price,$ProductClass1->getProduct(),$ProductClass1))
  129.                                                                 ->setDeliveryDays(intval($OptionCategory->getDeliveryDays()));
  130.                                         $optionPrice += $OrderItemOptionCategory->getPrice();
  131.                                         $optionTax += $OrderItemOptionCategory->getTax();
  132.                                     }
  133.                                     //dump($OrderItemOptionCategory);
  134.                                     $OrderItemOption->addOrderItemOptionCategory($OrderItemOptionCategory);
  135.                                     $this->entityManager->persist($OrderItemOptionCategory);
  136.                                 }
  137.                                 if(count($OrderItemOption->getOrderItemOptionCategories()) == 0)continue;
  138.                                 $item->addOrderItemOption($OrderItemOption);
  139.                                 $this->entityManager->persist($OrderItemOption);
  140.                             }
  141.                             $item->setPrice($item->getPrice() + $optionPrice);
  142.                             $item->setTax($item->getTax() + $optionTax);
  143.                             $item->setOptionSerial($CartItem->getOptionSerial());
  144.                             $item->setOptionSetFlg(true);
  145.                             $this->entityManager->persist($item);
  146.                             $this->entityManager->flush();
  147. /*
  148. dump($item);
  149. foreach($item->getOrderItemOptions() as $OrderItemOption){
  150.     dump($OrderItemOption);
  151.     foreach($OrderItemOption->getOrderItemOptionCategories() as $OrderItemOptionCategorie){
  152.         dump($OrderItemOptionCategorie);
  153.     }
  154. }
  155. */
  156.                             break 2;
  157.                         }
  158.                     }
  159.                 }
  160.             }else{
  161.                 if(!is_null($Plugin) && $Plugin->isEnabled() && $item->getOptionSetFlg() !== true){
  162.                     dump($item);
  163.                     $item->setPrice($item->getPrice() + $item->getOptionPrice());
  164.                     $item->setTax($item->getTax() + $item->getOptionTax());
  165.                     $item->setOptionSetFlg(true);
  166.                 }
  167.             }
  168.         }
  169.     }
  170. }