17 use Psr\Log\LoggerInterface;
243 private $dataObjectConverter;
311 $this->_eventManager = $eventManager;
312 $this->_coreRegistry = $coreRegistry;
313 $this->_salesConfig = $salesConfig;
314 $this->_session = $quoteSession;
316 $this->_objectCopyService = $objectCopyService;
322 $this->_metadataFormFactory = $metadataFormFactory;
325 $this->_scopeConfig = $scopeConfig;
339 parent::__construct(
$data);
341 ->get(ExtensibleDataObjectConverter::class);
352 $this->_isValidate = (bool)$flag;
391 $this->_coreRegistry->register(
395 'store_id' => $this->_session->getStore()->getId(),
396 'website_id' => $this->_session->getStore()->getWebsiteId(),
413 $this->_needCollect = $flag;
425 if ($this->_needCollectCart ===
true) {
445 if ($this->_needCollect) {
449 $this->quoteRepository->save($this->
getQuote());
470 if (!$this->_quote) {
471 $this->_quote = $this->
getSession()->getQuote();
501 $session->setData($order->getReordered() ?
'reordered' :
'order_id',
$order->getId());
509 foreach ($order->getItemsCollection($this->_salesConfig->getAvailableProductTypes(),
true) as
$orderItem) {
513 if (!
$order->getReordered()) {
519 if (is_string(
$item)) {
520 throw new \Magento\Framework\Exception\LocalizedException(
__(
$item));
540 $quote->getShippingAddress()->setShippingDescription(
$order->getShippingDescription());
542 $orderCouponCode =
$order->getCouponCode();
543 if ($orderCouponCode) {
544 $quote->setCouponCode($orderCouponCode);
547 if (
$quote->getCouponCode()) {
551 $this->_objectCopyService->copyFieldsetToTarget(
'sales_copy_order',
'to_edit',
$order,
$quote);
553 $this->_eventManager->dispatch(
'sales_convert_order_to_quote', [
'order' =>
$order,
'quote' =>
$quote]);
555 if (!
$order->getCustomerId()) {
556 $quote->setCustomerIsGuest(
true);
559 if (
$session->getUseOldShippingMethod(
true)) {
574 $quote->getShippingAddress()->unsCachedItemsAll();
575 $quote->setTotalsCollectedFlag(
false);
577 $this->quoteRepository->save(
$quote);
590 $this->
getQuote()->getBillingAddress()->setCustomerAddressId(
'');
591 $this->_objectCopyService->copyFieldsetToTarget(
592 'sales_copy_order_billing_address',
594 $order->getBillingAddress(),
595 $this->
getQuote()->getBillingAddress()
607 $orderShippingAddress =
$order->getShippingAddress();
611 $orderShippingAddress && $orderShippingAddress->getSameAsBilling()
613 $this->_objectCopyService->copyFieldsetToTarget(
614 'sales_copy_order_shipping_address',
616 $orderShippingAddress,
634 $product = $this->_objectManager->create(
635 \
Magento\Catalog\Model\Product::class
643 $product->setSkipCheckRequiredOption(
true);
645 if (is_numeric($qty)) {
649 if (is_string(
$item)) {
653 if ($additionalOptions =
$orderItem->getProductOptionByCode(
'additional_options')) {
657 'product' =>
$item->getProduct(),
658 'code' =>
'additional_options',
659 'value' => $this->serializer->serialize($additionalOptions)
665 $this->_eventManager->dispatch(
666 'sales_convert_order_item_to_quote_item',
683 if ($this->_wishlist !==
null && !$cacheReload) {
689 $this->_wishlist = $this->_objectManager->create(\
Magento\Wishlist\Model\Wishlist::class);
690 $this->_wishlist->loadByCustomerId(
$customerId,
true);
691 $this->_wishlist->setStore(
693 )->setSharedStoreIds(
694 $this->
getSession()->getStore()->getWebsite()->getStoreIds()
697 $this->_wishlist =
false;
710 if ($this->_cart !==
null) {
714 $this->_cart = $this->quoteFactory->create();
722 $this->_cart->setStore($this->
getSession()->getStore());
725 $this->quoteRepository->save($this->_cart);
739 if ($this->_compareList !==
null) {
744 $this->_compareList = $this->_objectManager->create(
748 $this->_compareList =
false;
785 $moveTo = explode(
'_', $moveTo);
786 switch ($moveTo[0]) {
791 $product = $this->_objectManager->create(
792 \
Magento\Catalog\Model\Product::class
796 $item->getProduct()->getId()
799 $product->setSkipCheckRequiredOption(
true);
802 if (is_string($newItem)) {
803 throw new \Magento\Framework\Exception\LocalizedException(
__($newItem));
805 $product->unsSkipCheckRequiredOption();
806 $newItem->checkData();
807 $this->_needCollectCart =
true;
811 if (
$cart &&
$item->getOptionByCode(
'additional_options') ===
null) {
813 $product = $this->_objectManager->create(
814 \
Magento\Catalog\Model\Product::class
818 $item->getProduct()->getId()
821 $info =
$item->getOptionByCode(
'info_buyRequest');
823 $info = new \Magento\Framework\DataObject(
824 $this->serializer->unserialize(
$info->getValue())
829 $info = new \Magento\Framework\DataObject(
840 throw new \Magento\Framework\Exception\LocalizedException(
__(
$cartItem));
843 $this->_needCollectCart =
true;
849 if (!isset($moveTo[1])) {
850 $wishlist = $this->_objectManager->create(
851 \
Magento\Wishlist\Model\Wishlist::class
857 $wishlist = $this->_objectManager->create(
858 \
Magento\Wishlist\Model\Wishlist::class
866 throw new \Magento\Framework\Exception\LocalizedException(
867 __(
'We can\'t find this wish list.')
872 )->setSharedStoreIds(
873 $this->
getSession()->getStore()->getWebsite()->getStoreIds()
876 if (
$wishlist->getId() &&
$item->getProduct()->isVisibleInSiteVisibility()) {
916 if (isset(
$data[
'add_order_item'])) {
917 foreach (
$data[
'add_order_item'] as $orderItemId =>
$value) {
921 if (is_string(
$item)) {
922 throw new \Magento\Framework\Exception\LocalizedException(
__(
$item));
926 if (isset(
$data[
'add_cart_item'])) {
927 foreach (
$data[
'add_cart_item'] as $itemId => $qty) {
935 if (isset(
$data[
'add_wishlist_item'])) {
936 foreach (
$data[
'add_wishlist_item'] as $itemId => $qty) {
937 $item = $this->_objectManager->create(
938 \
Magento\Wishlist\Model\Item::class
943 if (
$item->getId()) {
948 if (isset(
$data[
'add'])) {
953 if (isset(
$data[
'remove'])) {
954 foreach (
$data[
'remove'] as $itemId => $from) {
958 if (isset(
$data[
'empty_customer_cart']) && (
int)
$data[
'empty_customer_cart'] == 1) {
984 $cart->removeItem($itemId);
985 $this->_needCollectCart =
true;
991 $item = $this->_objectManager->create(\
Magento\Wishlist\Model\Item::class)->load($itemId);
996 $this->_objectManager->create(
998 )->load($itemId)->delete();
1038 $product = $this->_objectManager->create(
1039 \
Magento\Catalog\Model\Product::class
1048 throw new \Magento\Framework\Exception\LocalizedException(
1049 __(
'We could not add a product to cart by the ID "%1".',
$productId)
1056 if (is_string(
$item)) {
1057 throw new \Magento\Framework\Exception\LocalizedException(
__(
$item));
1078 $this->messageManager->addErrorMessage($e->getMessage());
1102 if (!empty(
$info[
'configured'])) {
1112 $this->quoteItemUpdater->update(
$item,
$info);
1121 $this->_logger->critical($e);
1144 )->getProductOptions();
1147 $newAdditionalOptions = [];
1149 foreach (explode(
"\n", $additionalOptions) as $_additionalOption) {
1150 if (strlen(trim($_additionalOption))) {
1152 if (strpos($_additionalOption,
':') ===
false) {
1153 throw new \Magento\Framework\Exception\LocalizedException(
1154 __(
'There is an error in one of the option rows.')
1157 list(
$label,
$value) = explode(
':', $_additionalOption, 2);
1159 throw new \Magento\Framework\Exception\LocalizedException(
1160 __(
'There is an error in one of the option rows.')
1173 $group = $this->_objectManager->get(
1185 if ($parsedValue !==
null) {
1188 $newAdditionalOptions[] = [
'label' =>
$label,
'value' =>
$value];
1191 $newAdditionalOptions[] = [
'label' =>
$label,
'value' =>
$value];
1196 return [
'options' => $newOptions,
'additional_options' => $newAdditionalOptions];
1215 $item->removeOption(
'option_ids');
1217 if (
$item->getOptionByCode(
'additional_options')) {
1218 $item->removeOption(
'additional_options');
1225 'product' =>
$item->getProduct(),
1226 'code' =>
'option_ids',
1227 'value' => implode(
',', array_keys(
$options[
'options']))
1236 'product' =>
$item->getProduct(),
1244 if (!empty(
$options[
'additional_options'])) {
1248 'product' =>
$item->getProduct(),
1249 'code' =>
'additional_options',
1250 'value' => $this->serializer->serialize(
$options[
'additional_options'])
1267 $newInfoOptions = [];
1274 $group = $this->_objectManager->get(
1288 return $newInfoOptions;
1299 $price = $this->_objectManager->get(\
Magento\Framework\Locale\FormatInterface::class)->getNumber(
$price);
1312 return $this->
getQuote()->getShippingAddress();
1323 $customerForm = $this->_metadataFormFactory->create(
1325 'adminhtml_checkout',
1326 $this->customerMapper->toFlatArray(
$customer),
1328 CustomerForm::IGNORE_INVISIBLE
1331 return $customerForm;
1352 $addressForm = $this->_metadataFormFactory->create(
1355 'adminhtml_customer_address',
1358 CustomerForm::DONT_IGNORE_INVISIBLE,
1364 if (
$address->getAddressType() == \Magento\Quote\Model\Quote\Address::TYPE_SHIPPING) {
1366 $requestScope =
'order/shipping_address';
1369 $requestScope =
'order/billing_address';
1376 if (
$address->getAddressType() == \Magento\Quote\Model\Quote\Address::TYPE_SHIPPING) {
1377 $typeName =
__(
'Shipping Address: ');
1379 $typeName =
__(
'Billing Address: ');
1382 $this->_errors[] = $typeName . $error;
1411 if (!$this->
getQuote()->isVirtual()) {
1418 $saveInAddressBook = (int)(!empty(
$address[
'save_in_address_book']));
1442 $tmpAddress->unsAddressId()->unsAddressType();
1443 $data = $tmpAddress->getData();
1444 $data[
'save_in_address_book'] = 0;
1460 return $this->
getQuote()->getBillingAddress();
1485 $saveInAddressBook = (int)(!empty(
$address[
'save_in_address_book']));
1490 $address[
'save_in_address_book'] = 0;
1498 $quote->getBillingAddress()->setCustomerAddressId(
null);
1539 $this->
getQuote()->getShippingAddress()->setCollectShippingRates(
true);
1552 $this->
getQuote()->collectTotals();
1575 if (!isset(
$data[
'method'])) {
1576 $data[
'method'] = $this->
getQuote()->getPayment()->getMethod();
1592 $this->
getQuote()->getShippingAddress()->setCollectShippingRates(
true);
1595 $this->
getQuote()->getShippingAddress()->setFreeShipping(0);
1597 $this->
getQuote()->setCouponCode($code);
1612 if (empty($accountData[
'email'])) {
1613 $accountData[
'email'] =
$customer->getEmail();
1618 $request = $form->prepareRequest($accountData);
1621 $customer = $this->customerFactory->create();
1622 $this->dataObjectHelper->populateWithArray(
1631 foreach ($form->getAttributes() as
$attribute) {
1638 if (isset(
$data[
'customer_group_id'])) {
1639 $customerGroup = $this->groupRepository->getById(
$data[
'customer_group_id']);
1640 $data[
'customer_tax_class_id'] = $customerGroup->getTaxClassId();
1659 if (is_array(
$data)) {
1665 if (isset(
$data[
'account'])) {
1669 if (isset(
$data[
'comment'])) {
1671 if (empty(
$data[
'comment'][
'customer_note_notify'])) {
1672 $this->
getQuote()->setCustomerNoteNotify(
false);
1674 $this->
getQuote()->setCustomerNoteNotify(
true);
1678 if (isset(
$data[
'billing_address'])) {
1682 if (isset(
$data[
'shipping_address'])) {
1686 if (isset(
$data[
'shipping_method'])) {
1690 if (isset(
$data[
'payment_method'])) {
1694 if (isset(
$data[
'coupon'][
'code'])) {
1712 || $this->accountManagement->isCustomerInStore(
$customer->getWebsiteId(),
$store->getId());
1727 $validationResults = $this->accountManagement->validate(
$customer);
1728 if (!$validationResults->isValid()) {
1729 $errors = $validationResults->getMessages();
1732 $this->_errors[] = $error;
1743 $this->dataObjectHelper->populateWithArray(
1761 public function _prepareCustomer()
1763 if ($this->
getQuote()->getCustomerIsGuest()) {
1774 ->setStoreId(
$store->getId())
1775 ->setWebsiteId(
$store->getWebsiteId())
1776 ->setCreatedAt(
null);
1780 $customerBillingAddressDataObject = $this->
getBillingAddress()->exportCustomerAddress();
1781 $customer->setSuffix($customerBillingAddressDataObject->getSuffix())
1782 ->setFirstname($customerBillingAddressDataObject->getFirstname())
1783 ->setLastname($customerBillingAddressDataObject->getLastname())
1784 ->setMiddlename($customerBillingAddressDataObject->getMiddlename())
1785 ->setPrefix($customerBillingAddressDataObject->getPrefix())
1786 ->setStoreId(
$store->getId())
1787 ->setWebsiteId(
$store->getWebsiteId())
1807 $origAddresses =
$customer->getAddresses();
1810 $customer->setAddresses($origAddresses);
1813 $quoteCode = sprintf(
'customer_%s',
$attribute->getAttributeCode());
1833 $quoteCustomerAddress->setCustomerId(
$customer->getId());
1835 $quoteAddressId = $quoteCustomerAddress->getCustomerAddressId();
1836 $addressType = $quoteCustomerAddress->getAddressType();
1837 if ($quoteAddressId) {
1839 $existingAddressDataObject = $this->addressRepository->getById($quoteAddressId);
1841 $this->dataObjectHelper->mergeDataObjects(
1842 get_class($existingAddressDataObject),
1843 $existingAddressDataObject,
1853 $isShippingAsBilling = $quoteCustomerAddress->getSameAsBilling();
1861 switch ($addressType) {
1862 case \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_BILLING:
1863 if (
$customer->getDefaultBilling() ===
null) {
1867 case \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING:
1868 if (
$customer->getDefaultShipping() ===
null) {
1873 throw new \InvalidArgumentException(
'Customer address type is invalid.');
1893 $addOptions =
$item->getOptionByCode(
'additional_options');
1895 $options[
'additional_options'] = $this->serializer->unserialize($addOptions->getValue());
1909 $this->_prepareCustomer();
1915 if ($this->
getSession()->getOrder()->getId()) {
1917 $originalId = $oldOrder->getOriginalIncrementId();
1919 $originalId = $oldOrder->getIncrementId();
1922 'original_increment_id' => $originalId,
1923 'relation_parent_id' => $oldOrder->getId(),
1924 'relation_parent_real_id' => $oldOrder->getIncrementId(),
1925 'edit_increment' => $oldOrder->getEditIncrement() + 1,
1926 'increment_id' => $originalId .
'-' . ($oldOrder->getEditIncrement() + 1)
1928 $quote->setReservedOrderId($orderData[
'increment_id']);
1930 $order = $this->quoteManagement->submit(
$quote, $orderData);
1932 if ($this->
getSession()->getOrder()->getId()) {
1934 $oldOrder->setRelationChildId(
$order->getId());
1935 $oldOrder->setRelationChildRealId(
$order->getIncrementId());
1937 $this->orderManagement->cancel($oldOrder->getEntityId());
1940 if ($this->getSendConfirmation()) {
1941 $this->emailSender->send(
$order);
1944 $this->_eventManager->dispatch(
'checkout_submit_all_after', [
'order' =>
$order,
'quote' =>
$quote]);
1957 protected function _validate()
1959 if (!$this->
getSession()->getStore()->getId()) {
1960 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Please select a store'));
1964 if (count(
$items) === 0) {
1965 $this->_errors[] =
__(
'Please specify order items.');
1970 $messages =
$item->getMessage(
false);
1971 if (
$item->getHasError() && is_array($messages) && !empty($messages)) {
1972 $this->_errors = array_merge($this->_errors, $messages);
1976 if (!$this->
getQuote()->isVirtual()) {
1978 $this->_errors[] =
__(
'The shipping method is missing. Select the shipping method and try again.');
1982 if (!$this->
getQuote()->getPayment()->getMethod()) {
1983 $this->_errors[] =
__(
"The payment method isn't selected. Enter the payment method and try again.");
1986 if (!
$method->isAvailable($this->getQuote())) {
1987 $this->_errors[] =
__(
'This payment method is not available.');
1991 }
catch (\
Magento\Framework\Exception\LocalizedException $e) {
1992 $this->_errors[] = $e->getMessage();
1996 if (!empty($this->_errors)) {
1999 foreach ($this->_errors as $error) {
2001 $this->messageManager->addErrorMessage($error);
2004 throw new \Magento\Framework\Exception\LocalizedException(
__(
'Validation is failed.'));
2017 return $this->
getData(
'account/email');
2026 private function isAddressesAreEqual(
Order $order)
2030 $shippingData = $this->dataObjectConverter->toFlatArray(
$shippingAddress, [], OrderAddressInterface::class);
2033 $shippingData[
'address_type'],
2034 $shippingData[
'entity_id'],
_prepareOptionsForRequest($item)
initFromOrder(\Magento\Sales\Model\Order $order)
$billingAddressDataObject
getData($key='', $index=null)
elseif(isset( $params[ 'redirect_parent']))
removeItem($itemId, $from)
_customerIsInStore($store)
const XML_PATH_DEFAULT_EMAIL_DOMAIN
setQuote(\Magento\Quote\Model\Quote $quote)
_assignOptionsToItem(\Magento\Quote\Model\Quote\Item $item, $options)
initFromOrderItem(\Magento\Sales\Model\Order\Item $orderItem, $qty=null)
setShippingAsBilling($flag)
_parseCustomPrice($price)
getCustomerWishlist($cacheReload=false)
addProducts(array $products)
setAccountData($accountData)
_setQuoteAddress(\Magento\Quote\Model\Quote\Address $address, array $data)
_parseOptions(\Magento\Quote\Model\Quote\Item $item, $additionalOptions)
moveQuoteItem($item, $moveTo, $qty)
_createCustomerForm(\Magento\Customer\Api\Data\CustomerInterface $customer)
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
setBillingAddress($address)
_validateCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
_initBillingAddressFromOrder(\Magento\Sales\Model\Order $order)
setShippingMethod($method)
setPaymentMethod($method)
_prepareCustomerAddress($customer, $quoteCustomerAddress)
__construct(\Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\Registry $coreRegistry, \Magento\Sales\Model\Config $salesConfig, \Magento\Backend\Model\Session\Quote $quoteSession, \Psr\Log\LoggerInterface $logger, \Magento\Framework\DataObject\Copy $objectCopyService, \Magento\Framework\Message\ManagerInterface $messageManager, Product\Quote\Initializer $quoteInitializer, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository, \Magento\Customer\Api\Data\AddressInterfaceFactory $addressFactory, \Magento\Customer\Model\Metadata\FormFactory $metadataFormFactory, \Magento\Customer\Api\GroupRepositoryInterface $groupRepository, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Sales\Model\AdminOrder\EmailSender $emailSender, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\Quote\Model\Quote\Item\Updater $quoteItemUpdater, \Magento\Framework\DataObject\Factory $objectFactory, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, \Magento\Customer\Api\AccountManagementInterface $accountManagement, \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory, \Magento\Customer\Model\Customer\Mapper $customerMapper, \Magento\Quote\Api\CartManagementInterface $quoteManagement, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, \Magento\Sales\Api\OrderManagementInterface $orderManagement, \Magento\Quote\Model\QuoteFactory $quoteFactory, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null, ExtensibleDataObjectConverter $dataObjectConverter=null)
foreach( $_productCollection as $_product)() ?>" class $info
setShippingAddress($address)
addProduct($product, $config=1)
_initShippingAddressFromOrder(\Magento\Sales\Model\Order $order)