/** * 配送商品情報を作成 * * @param Order $Order * @param Product $Product * @param ProductClass $ProductClass * @param $quantity * @return \Eccube\Entity\ShipmentItem */ public function getNewShipmentItem(Order $Order, Product $Product, ProductClass $ProductClass, $quantity) { $ShipmentItem = new ShipmentItem(); $shippings = $Order->getShippings(); // 選択された商品がどのお届け先情報と関連するかチェック $Shipping = null; foreach ($shippings as $s) { if ($s->getDelivery()->getProductType()->getId() == $ProductClass->getProductType()->getId()) { // 商品種別が同一のお届け先情報と関連させる $Shipping = $s; break; } } if (is_null($Shipping)) { // お届け先情報と関連していない場合、エラー throw new CartException('shopping.delivery.not.producttype'); } // 商品ごとの配送料合計 $productDeliveryFeeTotal = 0; if (!is_null($this->BaseInfo->getOptionProductDeliveryFee())) { $productDeliveryFeeTotal = $ProductClass->getDeliveryFee() * $quantity; } $Shipping->setShippingDeliveryFee($Shipping->getShippingDeliveryFee() + $productDeliveryFeeTotal); $ShipmentItem->setShipping($Shipping)->setOrder($Order)->setProductClass($ProductClass)->setProduct($Product)->setProductName($Product->getName())->setProductCode($ProductClass->getCode())->setPrice($ProductClass->getPrice02())->setQuantity($quantity); $ClassCategory1 = $ProductClass->getClassCategory1(); if (!is_null($ClassCategory1)) { $ShipmentItem->setClasscategoryName1($ClassCategory1->getName()); $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName()); } $ClassCategory2 = $ProductClass->getClassCategory2(); if (!is_null($ClassCategory2)) { $ShipmentItem->setClasscategoryName2($ClassCategory2->getName()); $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName()); } $Shipping->addShipmentItem($ShipmentItem); $this->em->persist($ShipmentItem); return $ShipmentItem; }
/** * 複数配送処理 */ public function shippingMultiple(Application $app, Request $request) { $cartService = $app['eccube.service.cart']; // カートチェック if (!$cartService->isLocked()) { // カートが存在しない、カートがロックされていない時はエラー return $app->redirect($app->url('cart')); } // カートチェック if (count($cartService->getCart()->getCartItems()) <= 0) { // カートが存在しない時はエラー return $app->redirect($app->url('cart')); } $Order = $app['eccube.service.shopping']->getOrder($app['config']['order_processing']); if (!$Order) { $app->addError('front.shopping.order.error'); return $app->redirect($app->url('shopping_error')); } // 複数配送時は商品毎でお届け先を設定する為、商品をまとめた数量を設定 $compItemQuantities = array(); foreach ($Order->getShippings() as $Shipping) { foreach ($Shipping->getShipmentItems() as $ShipmentItem) { $itemId = $ShipmentItem->getProductClass()->getId(); $quantity = $ShipmentItem->getQuantity(); if (array_key_exists($itemId, $compItemQuantities)) { $compItemQuantities[$itemId] = $compItemQuantities[$itemId] + $quantity; } else { $compItemQuantities[$itemId] = $quantity; } } } // 商品に紐づく商品情報を取得 $shipmentItems = array(); $productClassIds = array(); foreach ($Order->getShippings() as $Shipping) { foreach ($Shipping->getShipmentItems() as $ShipmentItem) { if (!in_array($ShipmentItem->getProductClass()->getId(), $productClassIds)) { $shipmentItems[] = $ShipmentItem; } $productClassIds[] = $ShipmentItem->getProductClass()->getId(); } } $form = $app->form()->getForm(); $form->add('shipping_multiple', 'collection', array('type' => 'shipping_multiple', 'data' => $shipmentItems, 'allow_add' => true, 'allow_delete' => true)); $form->handleRequest($request); $errors = array(); if ($form->isSubmitted() && $form->isValid()) { $data = $form['shipping_multiple']; // 数量が超えていないか、同一でないとエラー $itemQuantities = array(); foreach ($data as $mulitples) { /** @var \Eccube\Entity\ShipmentItem $multipleItem */ $multipleItem = $mulitples->getData(); foreach ($mulitples as $items) { foreach ($items as $item) { $quantity = $item['quantity']->getData(); $itemId = $multipleItem->getProductClass()->getId(); if (array_key_exists($itemId, $itemQuantities)) { $itemQuantities[$itemId] = $itemQuantities[$itemId] + $quantity; } else { $itemQuantities[$itemId] = $quantity; } } } } foreach ($compItemQuantities as $key => $value) { if (array_key_exists($key, $itemQuantities)) { if ($itemQuantities[$key] != $value) { $errors[] = array('message' => '数量の数が異なっています。'); // 対象がなければエラー return $app->render('Shopping/shipping_multiple.twig', array('form' => $form->createView(), 'shipmentItems' => $shipmentItems, 'compItemQuantities' => $compItemQuantities, 'errors' => $errors)); } } } // お届け先情報をdelete/insert $shippings = $Order->getShippings(); foreach ($shippings as $Shipping) { $Order->removeShipping($Shipping); $app['orm.em']->remove($Shipping); } foreach ($data as $mulitples) { /** @var \Eccube\Entity\ShipmentItem $multipleItem */ $multipleItem = $mulitples->getData(); foreach ($mulitples as $items) { foreach ($items as $item) { // 追加された配送先情報を作成 $Delivery = $multipleItem->getShipping()->getDelivery(); // 選択された情報を取得 $data = $item['customer_address']->getData(); if ($data instanceof CustomerAddress) { // 会員の場合、CustomerAddressオブジェクトを取得 $CustomerAddress = $data; } else { // 非会員の場合、選択されたindexが取得される $customerAddresses = $app['session']->get($this->sessionCustomerAddressKey); $customerAddresses = unserialize($customerAddresses); $CustomerAddress = $customerAddresses[$data]; $pref = $app['eccube.repository.master.pref']->find($CustomerAddress->getPref()->getId()); $CustomerAddress->setPref($pref); } $Shipping = new Shipping(); $Shipping->setFromCustomerAddress($CustomerAddress)->setDelivery($Delivery)->setDelFlg(Constant::DISABLED)->setOrder($Order); $app['orm.em']->persist($Shipping); $ProductClass = $multipleItem->getProductClass(); $Product = $multipleItem->getProduct(); $quantity = $item['quantity']->getData(); $ShipmentItem = new ShipmentItem(); $ShipmentItem->setShipping($Shipping)->setOrder($Order)->setProductClass($ProductClass)->setProduct($Product)->setProductName($Product->getName())->setProductCode($ProductClass->getCode())->setPrice($ProductClass->getPrice02())->setQuantity($quantity); $ClassCategory1 = $ProductClass->getClassCategory1(); if (!is_null($ClassCategory1)) { $ShipmentItem->setClasscategoryName1($ClassCategory1->getName()); $ShipmentItem->setClassName1($ClassCategory1->getClassName()->getName()); } $ClassCategory2 = $ProductClass->getClassCategory2(); if (!is_null($ClassCategory2)) { $ShipmentItem->setClasscategoryName2($ClassCategory2->getName()); $ShipmentItem->setClassName2($ClassCategory2->getClassName()->getName()); } $Shipping->addShipmentItem($ShipmentItem); $app['orm.em']->persist($ShipmentItem); // 配送料金の設定 $app['eccube.service.shopping']->setShippingDeliveryFee($Shipping); } } } // 配送先を更新 $app['orm.em']->flush(); return $app->redirect($app->url('shopping')); } return $app->render('Shopping/shipping_multiple.twig', array('form' => $form->createView(), 'shipmentItems' => $shipmentItems, 'compItemQuantities' => $compItemQuantities, 'errors' => $errors)); }