Пример #1
0
 /**
  * Function creates an empty cart.
  * This means an order with status 'in_cart' is created and all necessary data is set.
  *
  * @param UserInterface $user
  * @param bool $doPersist
  * @param string $locale
  * @param null|string $currencyCode
  *
  * @return OrderInterface
  */
 protected function createEmptyCart(UserInterface $user, $doPersist, $locale, $currencyCode = null)
 {
     $cart = $this->orderRepository->createNew();
     $cart->setCreator($user);
     $cart->setChanger($user);
     $cart->setCreated(new DateTime());
     $cart->setChanged(new DateTime());
     $cart->setOrderDate(new DateTime());
     // set currency - if not defined use default
     $currencyCode = $currencyCode ?: $this->defaultCurrency;
     $cart->setCurrencyCode($currencyCode);
     // get address from contact and account
     $contact = $user->getContact();
     $account = $contact->getMainAccount();
     $cart->setCustomerContact($contact);
     $cart->setCustomerAccount($account);
     /** Account $account */
     if ($account && $account->getResponsiblePerson()) {
         $cart->setResponsibleContact($account->getResponsiblePerson());
     }
     $addressSource = $contact;
     if ($account) {
         $addressSource = $account;
     }
     // get billing address
     $invoiceOrderAddress = null;
     $invoiceAddress = $this->accountManager->getBillingAddress($addressSource, true);
     if ($invoiceAddress) {
         // convert to order-address
         $invoiceOrderAddress = $this->orderAddressManager->getAndSetOrderAddressByContactAddress($invoiceAddress, $contact, $account);
         $cart->setInvoiceAddress($invoiceOrderAddress);
     }
     $deliveryOrderAddress = null;
     $deliveryAddress = $this->accountManager->getDeliveryAddress($addressSource, true);
     if ($deliveryAddress) {
         // convert to order-address
         $deliveryOrderAddress = $this->orderAddressManager->getAndSetOrderAddressByContactAddress($deliveryAddress, $contact, $account);
         $cart->setDeliveryAddress($deliveryOrderAddress);
     }
     // TODO: anonymous order
     // set order type
     if ($user) {
         $name = $user->getContact()->getFullName();
         $cart->setType($this->orderManager->getOrderTypeEntityById(OrderType::SHOP));
     } else {
         $name = 'Anonymous';
         $cart->setType($this->orderManager->getOrderTypeEntityById(OrderType::ANONYMOUS));
     }
     $cart->setCustomerName($name);
     $apiCart = $this->orderFactory->createApiEntity($cart, $locale);
     $this->orderManager->convertStatus($apiCart, OrderStatus::STATUS_IN_CART, false, $doPersist);
     if ($doPersist) {
         $this->em->persist($cart);
         if ($invoiceOrderAddress) {
             $this->em->persist($invoiceOrderAddress);
         }
         if ($deliveryOrderAddress) {
             $this->em->persist($deliveryOrderAddress);
         }
     }
     return $cart;
 }