Eccube\Entity\Shipping::setFromCustomerAddress PHP Method

setFromCustomerAddress() public method

CustomerAddress から個人情報を設定.
public setFromCustomerAddress ( CustomerAddress $CustomerAddress ) : Shipping
$CustomerAddress CustomerAddress
return Shipping
    public function setFromCustomerAddress(CustomerAddress $CustomerAddress)
    {
        $this->setName01($CustomerAddress->getName01())->setName02($CustomerAddress->getName02())->setKana01($CustomerAddress->getKana01())->setKana02($CustomerAddress->getKana02())->setCompanyName($CustomerAddress->getCompanyName())->setTel01($CustomerAddress->getTel01())->setTel02($CustomerAddress->getTel02())->setTel03($CustomerAddress->getTel03())->setFax01($CustomerAddress->getFax01())->setFax02($CustomerAddress->getFax02())->setFax03($CustomerAddress->getFax03())->setZip01($CustomerAddress->getZip01())->setZip02($CustomerAddress->getZip02())->setZipCode($CustomerAddress->getZip01() . $CustomerAddress->getZip02())->setPref($CustomerAddress->getPref())->setAddr01($CustomerAddress->getAddr01())->setAddr02($CustomerAddress->getAddr02());
        return $this;
    }

Usage Example

 /**
  * 複数配送処理
  */
 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));
 }