php Thelia-Model-AddressQuery类(方法)实例源码

下面列出了php Thelia-Model-AddressQuery 类(方法)源码代码实例,从而了解它的用法。

作者:alex6353    项目:theli   
public function testRenderLoop()
 {
     $customerId = CustomerQuery::create()->findOne()->getId();
     $this->handler->expects($this->any())->method("buildDataSet")->willReturn($this->handler->renderLoop("address", ["customer" => $customerId]));
     $lang = Lang::getDefaultLanguage();
     $loop = $this->handler->buildDataSet($lang);
     $this->assertInstanceOf("Thelia\\Core\\Template\\Loop\\Address", $loop);
     $data = $this->handler->buildData($lang);
     $addresses = AddressQuery::create()->filterByCustomerId($customerId)->find()->toArray("Id");
     foreach ($data->getData() as $row) {
         $this->assertArrayHasKey("id", $row);
         $this->assertArrayHasKey($row["id"], $addresses);
         $this->assertEquals(count($addresses), $row["loop_total"]);
         $address = $addresses[$row["id"]];
         $this->assertEquals($row["address1"], $address["Address1"]);
         $this->assertEquals($row["address2"], $address["Address2"]);
         $this->assertEquals($row["address3"], $address["Address3"]);
         $this->assertEquals($row["cellphone"], $address["Cellphone"]);
         $this->assertEquals($row["city"], $address["City"]);
         $this->assertEquals($row["company"], $address["Company"]);
         $this->assertEquals($row["country"], $address["CountryId"]);
         $this->assertEquals($row["create_date"], $address["CreatedAt"]);
         $this->assertEquals($row["update_date"], $address["UpdatedAt"]);
         $this->assertEquals($row["firstname"], $address["Firstname"]);
         $this->assertEquals($row["lastname"], $address["Lastname"]);
         $this->assertEquals($row["id"], $address["Id"]);
         $this->assertEquals($row["label"], $address["Label"]);
         $this->assertEquals($row["phone"], $address["Phone"]);
         $this->assertEquals($row["title"], $address["TitleId"]);
         $this->assertEquals($row["zipcode"], $address["Zipcode"]);
     }
 }

作者:zorn-    项目:theli   
public function buildModelCriteria()
 {
     $search = AddressQuery::create();
     $id = $this->getId();
     if (null !== $id && !in_array($id, array('*', 'any'))) {
         $search->filterById($id, Criteria::IN);
     }
     $customer = $this->getCustomer();
     if ($customer === 'current') {
         $currentCustomer = $this->securityContext->getCustomerUser();
         if ($currentCustomer === null) {
             return null;
         } else {
             $search->filterByCustomerId($currentCustomer->getId(), Criteria::EQUAL);
         }
     } else {
         $search->filterByCustomerId($customer, Criteria::EQUAL);
     }
     $default = $this->getDefault();
     if ($default === true) {
         $search->filterByIsDefault(1, Criteria::EQUAL);
     } elseif ($default === false) {
         $search->filterByIsDefault(0, Criteria::EQUAL);
     }
     $exclude = $this->getExclude();
     if (null !== $exclude && 'none' !== $exclude) {
         $search->filterById($exclude, Criteria::NOT_IN);
     }
     return $search;
 }

作者:fachriz    项目:theli   
protected function checkValidInvoice()
 {
     $order = $this->getSession()->getOrder();
     if (null === $order || null === $order->getChoosenInvoiceAddress() || null === $order->getPaymentModuleId() || null === AddressQuery::create()->findPk($order->getChoosenInvoiceAddress()) || null === ModuleQuery::create()->findPk($order->getPaymentModuleId())) {
         throw new RedirectException($this->retrieveUrlFromRouteId('order.invoice'));
     }
 }

作者:bcbr    项目:SoColissim   
/**
  * this method returns an array ***Thanks cap'tain obvious \(^.^)/***
  *->
  * @return array
  */
 public function buildArray()
 {
     // Find the address ... To find ! \m/
     $zipcode = $this->getZipcode();
     $city = $this->getCity();
     $address = $this->getAddress();
     $address = array("zipcode" => $zipcode, "city" => $city, "address" => "", "countrycode" => "FR");
     if (empty($zipcode) || empty($city)) {
         $search = AddressQuery::create();
         $customer = $this->securityContext->getCustomerUser();
         if ($customer !== null) {
             $search->filterByCustomerId($customer->getId());
             $search->filterByIsDefault("1");
         } else {
             throw new \ErrorException("Customer not connected.");
         }
         $search = $search->findOne();
         $address["zipcode"] = $search->getZipcode();
         $address["city"] = $search->getCity();
         $address["address"] = $search->getAddress1();
         $address["countrycode"] = $search->getCountry()->getIsoalpha2();
     }
     // Then ask the Web Service
     $request = new FindByAddress();
     $request->setAddress($address["address"])->setZipCode($address["zipcode"])->setCity($address["city"])->setCountryCode($address["countrycode"])->setFilterRelay("1")->setRequestId(md5(microtime()))->setLang("FR")->setOptionInter("1")->setShippingDate(date("d/m/Y"))->setAccountNumber(ConfigQuery::read('socolissimo_login'))->setPassword(ConfigQuery::read('socolissimo_pwd'));
     try {
         $response = $request->exec();
     } catch (InvalidArgumentException $e) {
         $response = array();
     } catch (\SoapFault $e) {
         $response = array();
     }
     return $response;
 }

作者:GuiminZHO    项目:theli   
public function testFormatAddress()
 {
     // Test for address in France
     $countryFR = CountryQuery::create()->filterByIsoalpha2('FR')->findOne();
     $address = AddressQuery::create()->findOne();
     $address->setCountryId($countryFR->getId())->save();
     $data = $this->renderString('{format_address address=$address locale="fr_FR"}', ['address' => $address->getId()]);
     $title = $address->getCustomerTitle()->setLocale('fr_FR')->getShort();
     $expected = ['<p >', sprintf('<span class="recipient">%s %s %s</span><br>', $title, $address->getLastname(), $address->getFirstname()), sprintf('<span class="address-line1">%s</span><br>', $address->getAddress1()), sprintf('<span class="postal-code">%s</span> <span class="locality">%s</span><br>', $address->getZipcode(), $address->getCity()), '<span class="country">France</span>', '</p>'];
     $this->assertEquals($data, implode("\n", $expected));
     // Test for address in USA
     $stateDC = StateQuery::create()->filterByIsocode('DC')->findOne();
     $countryUS = $stateDC->getCountry();
     $address->setCountryId($countryUS->getId())->setStateId($stateDC->getId())->save();
     $data = $this->renderString('{format_address address=$address locale="en_US"}', ['address' => $address->getId()]);
     $title = $address->getCustomerTitle()->setLocale('en_US')->getShort();
     $expected = ['<p >', sprintf('<span class="recipient">%s %s %s</span><br>', $title, $address->getLastname(), $address->getFirstname()), sprintf('<span class="address-line1">%s</span><br>', $address->getAddress1()), sprintf('<span class="locality">%s</span>, <span class="administrative-area">%s</span> <span class="postal-code">%s</span><br>', $address->getCity(), $stateDC->getIsocode(), $address->getZipcode()), '<span class="country">United States</span>', '</p>'];
     $this->assertEquals($data, implode("\n", $expected));
     // Test html tag
     $data = $this->renderString('{format_address html_tag="address" html_class="a_class" html_id="an_id" address=$address}', ['address' => $address->getId()]);
     $this->assertTrue(strpos($data, '<address class="a_class" id="an_id">') !== false);
     // Test plain text
     $data = $this->renderString('{format_address html="0" address=$address locale="en_US"}', ['address' => $address->getId()]);
     $expected = [sprintf('%s %s %s', $title, $address->getLastname(), $address->getFirstname()), sprintf('%s', $address->getAddress1()), sprintf('%s, %s %s', $address->getCity(), $stateDC->getIsocode(), $address->getZipcode()), 'United States'];
     $this->assertEquals($data, implode("\n", $expected));
 }

作者:marger    项目:theli   
public function verifyDeliveryAddress($value, ExecutionContextInterface $context)
 {
     $address = AddressQuery::create()->findPk($value);
     if (null === $address) {
         $context->addViolation(Translator::getInstance()->trans("Address ID not found"));
     }
 }

作者:lopes-vincen    项目:DpdPicku   
public function isModuleDpdPickup(OrderEvent $event)
 {
     $address = AddressIcirelaisQuery::create()->findPk($event->getDeliveryAddress());
     if ($this->check_module($event->getDeliveryModule())) {
         //tmp solution
         $request = $this->getRequest();
         $pr_code = $request->request->get('pr_code');
         if (!empty($pr_code)) {
             // Get details w/ SOAP
             $con = new \SoapClient(__DIR__ . "/../Config/exapaq.wsdl", array('soap_version' => SOAP_1_2));
             $response = $con->GetPudoDetails(array("pudo_id" => $pr_code));
             $xml = new \SimpleXMLElement($response->GetPudoDetailsResult->any);
             if (isset($xml->ERROR)) {
                 throw new \ErrorException("Error while choosing pick-up & go store: " . $xml->ERROR);
             }
             $customer_name = AddressQuery::create()->findPk($event->getDeliveryAddress());
             $request->getSession()->set('DpdPickupDeliveryId', $event->getDeliveryAddress());
             if ($address === null) {
                 $address = new AddressIcirelais();
                 $address->setId($event->getDeliveryAddress());
             }
             // France Métropolitaine
             $address->setCode($pr_code)->setCompany((string) $xml->PUDO_ITEMS->PUDO_ITEM->NAME)->setAddress1((string) $xml->PUDO_ITEMS->PUDO_ITEM->ADDRESS1)->setAddress2((string) $xml->PUDO_ITEMS->PUDO_ITEM->ADDRESS2)->setAddress3((string) $xml->PUDO_ITEMS->PUDO_ITEM->ADDRESS3)->setZipcode((string) $xml->PUDO_ITEMS->PUDO_ITEM->ZIPCODE)->setCity((string) $xml->PUDO_ITEMS->PUDO_ITEM->CITY)->setFirstname($customer_name->getFirstname())->setLastname($customer_name->getLastname())->setTitleId($customer_name->getTitleId())->setCountryId($customer_name->getCountryId())->save();
         } else {
             throw new \ErrorException("No pick-up & go store chosen for DpdPickup delivery module");
         }
     } elseif (null !== $address) {
         $address->delete();
     }
 }

作者:marger    项目:theli   
/**
  * Return an Address a CouponManager can process
  *
  * @return \Thelia\Model\Address
  */
 public function getDeliveryAddress()
 {
     try {
         return AddressQuery::create()->findPk($this->getRequest()->getSession()->getOrder()->getChoosenDeliveryAddress());
     } catch (\Exception $ex) {
         throw new \LogicException("Failed to get delivery address (" . $ex->getMessage() . ")");
     }
 }

作者:vigourouxjulie    项目:theli   
/**
  * Coupon consuming
  */
 public function consumeAction()
 {
     $this->checkCartNotEmpty();
     $message = false;
     $couponCodeForm = $this->createForm(FrontForm::COUPON_CONSUME);
     try {
         $form = $this->validateForm($couponCodeForm, 'post');
         $couponCode = $form->get('coupon-code')->getData();
         if (null === $couponCode || empty($couponCode)) {
             $message = true;
             throw new \Exception($this->getTranslator()->trans('Coupon code can\'t be empty', [], Front::MESSAGE_DOMAIN));
         }
         $couponConsumeEvent = new CouponConsumeEvent($couponCode);
         // Dispatch Event to the Action
         $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent);
         /* recalculate postage amount */
         $order = $this->getSession()->getOrder();
         if (null !== $order) {
             $deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
             $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
             if (null !== $deliveryModule && null !== $deliveryAddress) {
                 $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
                 $orderEvent = new OrderEvent($order);
                 try {
                     $postage = OrderPostage::loadFromPostage($moduleInstance->getPostage($deliveryAddress->getCountry()));
                     $orderEvent->setPostage($postage->getAmount());
                     $orderEvent->setPostageTax($postage->getAmountTax());
                     $orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
                     $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
                 } catch (DeliveryException $ex) {
                     // The postage has been chosen, but changes dues to coupon causes an exception.
                     // Reset the postage data in the order
                     $orderEvent->setDeliveryModule(0);
                     $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
                 }
             }
         }
         return $this->generateSuccessRedirect($couponCodeForm);
     } catch (FormValidationException $e) {
         $message = $this->getTranslator()->trans('Please check your coupon code: %message', ["%message" => $e->getMessage()], Front::MESSAGE_DOMAIN);
     } catch (UnmatchableConditionException $e) {
         $message = $this->getTranslator()->trans('You should <a href="%sign">sign in</a> or <a href="%register">register</a> to use this coupon', ['%sign' => $this->retrieveUrlFromRouteId('customer.login.view'), '%register' => $this->retrieveUrlFromRouteId('customer.create.view')], Front::MESSAGE_DOMAIN);
     } catch (PropelException $e) {
         $this->getParserContext()->setGeneralError($e->getMessage());
     } catch (\Exception $e) {
         $message = $this->getTranslator()->trans('Sorry, an error occurred: %message', ["%message" => $e->getMessage()], Front::MESSAGE_DOMAIN);
     }
     if ($message !== false) {
         Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage()));
         $couponCodeForm->setErrorMessage($message);
         $this->getParserContext()->addForm($couponCodeForm)->setGeneralError($message);
     }
     return $this->generateErrorRedirect($couponCodeForm);
 }

作者:bcbr    项目:LocalPicku   
/**
  * this method returns an array
  *
  * @return array
  */
 public function buildArray()
 {
     $id = $this->getId();
     /** @var \Thelia\Core\HttpFoundation\Session\Session $session */
     $session = $this->container->get('request')->getSession();
     $address = AddressQuery::create()->filterByCustomerId($session->getCustomerUser()->getId())->findPk($id);
     if ($address === null) {
         throw new Exception("The requested address doesn't exist");
     }
     /** @var \Thelia\Model\Customer $customer */
     $customer = $session->getCustomerUser();
     return array('Id' => '0', 'Label' => $address->getLabel(), 'CustomerId' => $address->getCustomerId(), 'TitleId' => $address->getTitleId(), 'Company' => ConfigQuery::read('store_name'), 'Firstname' => $customer->getFirstname(), 'Lastname' => $customer->getLastname(), 'Address1' => ConfigQuery::read('store_address1'), 'Address2' => ConfigQuery::read('store_address2'), 'Address3' => ConfigQuery::read('store_address3'), 'Zipcode' => ConfigQuery::read('store_zipcode'), 'City' => ConfigQuery::read('store_city'), 'CountryId' => ConfigQuery::read('store_country'), 'Phone' => $address->getPhone(), 'Cellphone' => $address->getCellphone(), 'IsDefault' => '0');
 }

作者:zorn-    项目:theli   
protected function migrateAddress(MigrateCountryEvent $event)
 {
     $con = Propel::getWriteConnection(AddressTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         $updatedRows = AddressQuery::create()->filterByCountryId($event->getCountry())->update(['CountryId' => $event->getNewCountry(), 'StateId' => $event->getNewState()]);
         $con->commit();
         return $updatedRows;
     } catch (PropelException $e) {
         $con->rollback();
         throw $e;
     }
 }

作者:alex6353    项目:theli   
public function checkValidDeliveryFunction($params, &$smarty)
 {
     $order = $this->request->getSession()->getOrder();
     /* Does address and module still exists ? We assume address owner can't change neither module type */
     if ($order !== null) {
         $checkAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
         $checkModule = ModuleQuery::create()->findPk($order->getDeliveryModuleId());
     }
     if (null === $order || null == $checkAddress || null === $checkModule) {
         throw new OrderException('Delivery must be defined', OrderException::UNDEFINED_DELIVERY, array('missing' => 1));
     }
     return "";
 }

作者:alex6353    项目:theli   
/**
  * Coupon consuming
  */
 public function consumeAction()
 {
     $this->checkAuth();
     $this->checkCartNotEmpty();
     $message = false;
     $couponCodeForm = new CouponCode($this->getRequest());
     try {
         $form = $this->validateForm($couponCodeForm, 'post');
         $couponCode = $form->get('coupon-code')->getData();
         if (null === $couponCode || empty($couponCode)) {
             $message = true;
             throw new \Exception('Coupon code can\'t be empty');
         }
         $couponConsumeEvent = new CouponConsumeEvent($couponCode);
         // Dispatch Event to the Action
         $this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent);
         /* recalculate postage amount */
         $order = $this->getSession()->getOrder();
         if (null !== $order) {
             $deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
             $deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
             if (null !== $deliveryModule && null !== $deliveryAddress) {
                 $moduleInstance = $deliveryModule->getModuleInstance($this->container);
                 $orderEvent = new OrderEvent($order);
                 try {
                     $postage = $moduleInstance->getPostage($deliveryAddress->getCountry());
                     $orderEvent->setPostage($postage);
                     $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
                 } catch (DeliveryException $ex) {
                     // The postage has been chosen, but changes dues to coupon causes an exception.
                     // Reset the postage data in the order
                     $orderEvent->setDeliveryModule(0);
                     $this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
                 }
             }
         }
         return $this->generateSuccessRedirect($couponCodeForm);
     } catch (FormValidationException $e) {
         $message = sprintf('Please check your coupon code: %s', $e->getMessage());
     } catch (PropelException $e) {
         $this->getParserContext()->setGeneralError($e->getMessage());
     } catch (\Exception $e) {
         $message = sprintf('Sorry, an error occurred: %s', $e->getMessage());
     }
     if ($message !== false) {
         Tlog::getInstance()->error(sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage()));
         $couponCodeForm->setErrorMessage($message);
         $this->getParserContext()->addForm($couponCodeForm)->setGeneralError($message);
     }
 }

作者:JumBa    项目:ImportT   
public function preImport()
 {
     // Empty address, customer and customer title table
     OrderQuery::create()->deleteAll();
     AddressQuery::create()->deleteAll();
     OrderAddressQuery::create()->deleteAll();
     CustomerQuery::create()->deleteAll();
     // Also empty url rewriting table
     $con = Propel::getConnection(RewritingUrlTableMap::DATABASE_NAME);
     $con->exec('SET FOREIGN_KEY_CHECKS=0');
     RewritingUrlQuery::create()->deleteAll();
     $con->exec('SET FOREIGN_KEY_CHECKS=1');
     $this->cust_corresp->reset();
     if ($this->thelia_version > 150) {
         $this->importCustomerTitle();
     }
 }

作者:lopes-vincen    项目:DpdPicku   
public function buildModelCriteria()
 {
     $zipcode = $this->getZipcode();
     $city = $this->getCity();
     if (!empty($zipcode) and !empty($city)) {
         $this->zipcode = $zipcode;
         $this->city = $city;
         $this->addressflag = false;
     } else {
         $search = AddressQuery::create();
         $customer = $this->securityContext->getCustomerUser();
         if ($customer !== null) {
             $search->filterByCustomerId($customer->getId());
             $search->filterByIsDefault("1");
         } else {
             throw new \ErrorException("Customer not connected.");
         }
         return $search;
     }
 }

作者:zorn-    项目:theli   
public function getDeliveryModuleListAjaxAction()
 {
     $this->checkXmlHttpRequest();
     // Change the delivery address if customer has changed it
     $session = $this->getSession();
     $addressId = $this->getRequest()->get('address_id', null);
     if (null !== $addressId && $addressId !== $session->getOrder()->getChoosenDeliveryAddress()) {
         $address = AddressQuery::create()->findPk($addressId);
         if (null !== $address && $address->getCustomerId() === $session->getCustomerUser()->getId()) {
             $session->getOrder()->setChoosenDeliveryAddress($addressId);
         }
     }
     // seems to be not necessary anymore since we have the new adress selected.
     // keep for compatibility with old template
     // TODO remove in version 2.3
     $countryId = $this->getRequest()->get('country_id', $this->container->get('thelia.taxEngine')->getDeliveryCountry()->getId());
     $state = $this->container->get('thelia.taxEngine')->getDeliveryState();
     $stateId = $state !== null ? $state->getId() : null;
     $stateId = $this->getRequest()->get('state_id', $stateId);
     $args = array('country' => $countryId, 'state' => $stateId, 'address' => $session->getOrder()->getChoosenDeliveryAddress());
     return $this->render('ajax/order-delivery-module-list', $args);
 }

作者:shiron    项目:theli   
/**
  * If this collection has already been initialized with
  * an identical criteria, it returns the collection.
  * Otherwise if this Customer is new, it will return
  * an empty collection; or if this Customer has previously
  * been saved, it will retrieve related Addresses from storage.
  *
  * This method is protected by default in order to keep the public
  * api reasonable.  You can provide public methods for those you
  * actually need in Customer.
  *
  * @param      Criteria $criteria optional Criteria object to narrow the query
  * @param      ConnectionInterface $con optional connection object
  * @param      string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN)
  * @return Collection|ChildAddress[] List of ChildAddress objects
  */
 public function getAddressesJoinCountry($criteria = null, $con = null, $joinBehavior = Criteria::LEFT_JOIN)
 {
     $query = ChildAddressQuery::create(null, $criteria);
     $query->joinWith('Country', $joinBehavior);
     return $this->getAddresses($query, $con);
 }

作者:NandoKstroNe    项目:theli   
/**
  * Load an existing object from the database
  */
 protected function getExistingObject()
 {
     return AddressQuery::create()->findPk($this->getRequest()->get('address_id'));
 }

作者:alex6353    项目:theli   
public function deleteAction($address_id)
 {
     $this->checkAuth();
     $error_message = false;
     $customer = $this->getSecurityContext()->getCustomerUser();
     $address = AddressQuery::create()->findPk($address_id);
     if (!$address || $customer->getId() != $address->getCustomerId()) {
         // If Ajax Request
         if ($this->getRequest()->isXmlHttpRequest()) {
             return $this->jsonResponse(json_encode(array("success" => false, "message" => "Error during address deletion process")));
         } else {
             return $this->generateRedirectFromRoute('default');
         }
     }
     try {
         $this->dispatch(TheliaEvents::ADDRESS_DELETE, new AddressEvent($address));
     } catch (\Exception $e) {
         $error_message = $e->getMessage();
     }
     \Thelia\Log\Tlog::getInstance()->error(sprintf('Error during address deletion : %s', $error_message));
     // If Ajax Request
     if ($this->getRequest()->isXmlHttpRequest()) {
         if ($error_message) {
             $response = $this->jsonResponse(json_encode(array("success" => false, "message" => $error_message)));
         } else {
             $response = $this->jsonResponse(json_encode(array("success" => true, "message" => "")));
         }
         return $response;
     } else {
         return $this->generateRedirectFromRoute('default', array('view' => 'account'));
     }
 }

作者:badela    项目:theli   
/**
  * @param EventDispatcherInterface $dispatcher
  * @param ModelOrder $sessionOrder
  * @param CurrencyModel $currency
  * @param LangModel $lang
  * @param CartModel $cart
  * @param UserInterface $customer
  * @param bool $manageStock decrement stock when order is created if true
  * @return ModelOrder
  * @throws \Exception
  * @throws \Propel\Runtime\Exception\PropelException
  */
 protected function createOrder(EventDispatcherInterface $dispatcher, ModelOrder $sessionOrder, CurrencyModel $currency, LangModel $lang, CartModel $cart, UserInterface $customer, $manageStock)
 {
     $con = Propel::getConnection(OrderTableMap::DATABASE_NAME);
     $con->beginTransaction();
     $placedOrder = $sessionOrder->copy();
     $placedOrder->setDispatcher($dispatcher);
     $deliveryAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenDeliveryAddress());
     $taxCountry = $deliveryAddress->getCountry();
     $invoiceAddress = AddressQuery::create()->findPk($sessionOrder->getChoosenInvoiceAddress());
     $cartItems = $cart->getCartItems();
     /* fulfill order */
     $placedOrder->setCustomerId($customer->getId());
     $placedOrder->setCurrencyId($currency->getId());
     $placedOrder->setCurrencyRate($currency->getRate());
     $placedOrder->setLangId($lang->getId());
     /* hard save the delivery and invoice addresses */
     $deliveryOrderAddress = new OrderAddress();
     $deliveryOrderAddress->setCustomerTitleId($deliveryAddress->getTitleId())->setCompany($deliveryAddress->getCompany())->setFirstname($deliveryAddress->getFirstname())->setLastname($deliveryAddress->getLastname())->setAddress1($deliveryAddress->getAddress1())->setAddress2($deliveryAddress->getAddress2())->setAddress3($deliveryAddress->getAddress3())->setZipcode($deliveryAddress->getZipcode())->setCity($deliveryAddress->getCity())->setPhone($deliveryAddress->getPhone())->setCountryId($deliveryAddress->getCountryId())->save($con);
     $invoiceOrderAddress = new OrderAddress();
     $invoiceOrderAddress->setCustomerTitleId($invoiceAddress->getTitleId())->setCompany($invoiceAddress->getCompany())->setFirstname($invoiceAddress->getFirstname())->setLastname($invoiceAddress->getLastname())->setAddress1($invoiceAddress->getAddress1())->setAddress2($invoiceAddress->getAddress2())->setAddress3($invoiceAddress->getAddress3())->setZipcode($invoiceAddress->getZipcode())->setCity($invoiceAddress->getCity())->setPhone($invoiceAddress->getPhone())->setCountryId($invoiceAddress->getCountryId())->save($con);
     $placedOrder->setDeliveryOrderAddressId($deliveryOrderAddress->getId());
     $placedOrder->setInvoiceOrderAddressId($invoiceOrderAddress->getId());
     $placedOrder->setStatusId(OrderStatusQuery::getNotPaidStatus()->getId());
     $placedOrder->setCartId($cart->getId());
     /* memorize discount */
     $placedOrder->setDiscount($cart->getDiscount());
     $placedOrder->save($con);
     /* fulfill order_products and decrease stock */
     foreach ($cartItems as $cartItem) {
         $product = $cartItem->getProduct();
         /* get translation */
         /** @var ProductI18n $productI18n */
         $productI18n = I18n::forceI18nRetrieving($lang->getLocale(), 'Product', $product->getId());
         $pse = $cartItem->getProductSaleElements();
         // get the virtual document path
         $virtualDocumentEvent = new VirtualProductOrderHandleEvent($placedOrder, $pse->getId());
         // essentially used for virtual product. modules that handles virtual product can
         // allow the use of stock even for virtual products
         $useStock = true;
         $virtual = 0;
         // if the product is virtual, dispatch an event to collect information
         if ($product->getVirtual() === 1) {
             $dispatcher->dispatch(TheliaEvents::VIRTUAL_PRODUCT_ORDER_HANDLE, $virtualDocumentEvent);
             $useStock = $virtualDocumentEvent->isUseStock();
             $virtual = $virtualDocumentEvent->isVirtual() ? 1 : 0;
         }
         /* check still in stock */
         if ($cartItem->getQuantity() > $pse->getQuantity() && true === ConfigQuery::checkAvailableStock() && $useStock) {
             throw new TheliaProcessException("Not enough stock", TheliaProcessException::CART_ITEM_NOT_ENOUGH_STOCK, $cartItem);
         }
         if ($useStock && $manageStock) {
             /* decrease stock for non virtual product */
             $allowNegativeStock = intval(ConfigQuery::read('allow_negative_stock', 0));
             $newStock = $pse->getQuantity() - $cartItem->getQuantity();
             //Forbid negative stock
             if ($newStock < 0 && 0 === $allowNegativeStock) {
                 $newStock = 0;
             }
             $pse->setQuantity($newStock);
             $pse->save($con);
         }
         /* get tax */
         /** @var TaxRuleI18n $taxRuleI18n */
         $taxRuleI18n = I18n::forceI18nRetrieving($lang->getLocale(), 'TaxRule', $product->getTaxRuleId());
         $taxDetail = $product->getTaxRule()->getTaxDetail($product, $taxCountry, $cartItem->getPrice(), $cartItem->getPromoPrice(), $lang->getLocale());
         $orderProduct = new OrderProduct();
         $orderProduct->setOrderId($placedOrder->getId())->setProductRef($product->getRef())->setProductSaleElementsRef($pse->getRef())->setProductSaleElementsId($pse->getId())->setTitle($productI18n->getTitle())->setChapo($productI18n->getChapo())->setDescription($productI18n->getDescription())->setPostscriptum($productI18n->getPostscriptum())->setVirtual($virtual)->setVirtualDocument($virtualDocumentEvent->getPath())->setQuantity($cartItem->getQuantity())->setPrice($cartItem->getPrice())->setPromoPrice($cartItem->getPromoPrice())->setWasNew($pse->getNewness())->setWasInPromo($cartItem->getPromo())->setWeight($pse->getWeight())->setTaxRuleTitle($taxRuleI18n->getTitle())->setTaxRuleDescription($taxRuleI18n->getDescription())->setEanCode($pse->getEanCode())->setCartItemId($cartItem->getId())->setDispatcher($dispatcher)->save($con);
         /* fulfill order_product_tax */
         /** @var OrderProductTax $tax */
         foreach ($taxDetail as $tax) {
             $tax->setOrderProductId($orderProduct->getId());
             $tax->save($con);
         }
         /* fulfill order_attribute_combination and decrease stock */
         foreach ($pse->getAttributeCombinations() as $attributeCombination) {
             /** @var \Thelia\Model\Attribute $attribute */
             $attribute = I18n::forceI18nRetrieving($lang->getLocale(), 'Attribute', $attributeCombination->getAttributeId());
             /** @var \Thelia\Model\AttributeAv $attributeAv */
             $attributeAv = I18n::forceI18nRetrieving($lang->getLocale(), 'AttributeAv', $attributeCombination->getAttributeAvId());
             $orderAttributeCombination = new OrderProductAttributeCombination();
             $orderAttributeCombination->setOrderProductId($orderProduct->getId())->setAttributeTitle($attribute->getTitle())->setAttributeChapo($attribute->getChapo())->setAttributeDescription($attribute->getDescription())->setAttributePostscriptum($attribute->getPostscriptum())->setAttributeAvTitle($attributeAv->getTitle())->setAttributeAvChapo($attributeAv->getChapo())->setAttributeAvDescription($attributeAv->getDescription())->setAttributeAvPostscriptum($attributeAv->getPostscriptum())->save($con);
         }
     }
     $con->commit();
     return $placedOrder;
 }


问题


面经


文章

微信
公众号

扫码关注公众号