/** * {@inheritdoc} */ public function create($orderId) { $order = $this->orderRepository->get($orderId); if ($order->getCustomerId()) { throw new AlreadyExistsException(__("This order already has associated customer account")); } $customerData = $this->objectCopyService->copyFieldsetToTarget('order_address', 'to_customer', $order->getBillingAddress(), []); $addresses = $order->getAddresses(); foreach ($addresses as $address) { $addressData = $this->objectCopyService->copyFieldsetToTarget('order_address', 'to_customer_address', $address, []); /** @var \Magento\Customer\Api\Data\AddressInterface $customerAddress */ $customerAddress = $this->addressFactory->create(['data' => $addressData]); if (is_string($address->getRegion())) { /** @var \Magento\Customer\Api\Data\RegionInterface $region */ $region = $this->regionFactory->create(); $region->setRegion($address->getRegion()); $region->setRegionCode($address->getRegionCode()); $region->setRegionId($address->getRegionId()); $customerAddress->setRegion($region); } $customerData['addresses'][] = $customerAddress; } /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */ $customer = $this->customerFactory->create(['data' => $customerData]); $account = $this->accountManagement->createAccount($customer); $order->setCustomerId($account->getId()); $this->orderRepository->save($order); return $account; }
/** * @param Address $object * @param array $data * @return OrderInterface */ public function convert(Address $object, $data = []) { $orderData = $this->objectCopyService->getDataFromFieldset('quote_convert_address', 'to_order', $object); $order = $this->orderFactory->create(); $this->dataObjectHelper->populateWithArray($order, array_merge($orderData, $data), '\\Magento\\Sales\\Api\\Data\\OrderInterface'); $order->setStoreId($object->getQuote()->getStoreId())->setQuoteId($object->getQuote()->getId()); $this->objectCopyService->copyFieldsetToTarget('sales_convert_quote', 'to_order', $object->getQuote(), $order); $this->eventManager->dispatch('sales_convert_quote_to_order', ['order' => $order, 'quote' => $object->getQuote()]); return $order; }
public function testCopyFieldsetArrayTarget() { $fieldset = 'sales_copy_order'; $aspect = 'to_edit'; $data = array('customer_email' => '*****@*****.**', 'customer_group_id' => '1'); $source = new \Magento\Framework\Object($data); $target = array(); $expectedTarget = $data; $this->assertEquals($target, $this->_service->copyFieldsetToTarget('invalid_fieldset', $aspect, $source, $target)); $this->assertEquals($expectedTarget, $this->_service->copyFieldsetToTarget($fieldset, $aspect, $source, $target)); }
public function testGetCopyFieldsetToTargetWhenTargetIsArray() { $fields['code']['aspect'] = 'value'; $target['code'] = []; $this->fieldsetConfigMock->expects($this->once())->method('getFieldset')->with('fieldset', 'global')->will($this->returnValue($fields)); $this->sourceMock->expects($this->once())->method('getDataUsingMethod')->with('code')->will($this->returnValue('value')); $this->targetMock->expects($this->never())->method('setDataUsingMethod'); $eventName = sprintf('core_copy_fieldset_%s_%s', 'fieldset', 'aspect'); $newTarget = ['code' => [], 'value' => 'value']; $data = ['target' => $newTarget, 'source' => $this->sourceMock, 'root' => 'global']; $this->eventManagerMock->expects($this->once())->method('dispatch')->with($eventName, $data); $this->assertEquals($newTarget, $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $this->sourceMock, $target)); }
/** * Convert order item object to Creditmemo item * * @param \Magento\Sales\Model\Order\Item $item * @return \Magento\Sales\Model\Order\Creditmemo\Item */ public function itemToCreditmemoItem(\Magento\Sales\Model\Order\Item $item) { $creditmemoItem = $this->_creditmemoItemFactory->create(); $creditmemoItem->setOrderItem($item)->setProductId($item->getProductId()); $this->_objectCopyService->copyFieldsetToTarget('sales_convert_order_item', 'to_cm_item', $item, $creditmemoItem); return $creditmemoItem; }
/** * Convert quote item to order item * * @param \Magento\Sales\Model\Quote\Item\AbstractItem $item * @return \Magento\Sales\Model\Order\Item */ public function itemToOrderItem(\Magento\Sales\Model\Quote\Item\AbstractItem $item) { $orderItem = $this->_orderItemFactory->create()->setStoreId($item->getStoreId())->setQuoteItemId($item->getId())->setQuoteParentItemId($item->getParentItemId())->setProductId($item->getProductId())->setProductType($item->getProductType())->setQtyBackordered($item->getBackorders())->setProduct($item->getProduct())->setBaseOriginalPrice($item->getBaseOriginalPrice()); $options = $item->getProductOrderOptions(); if (!$options) { $options = $item->getProduct()->getTypeInstance()->getOrderOptions($item->getProduct()); } $orderItem->setProductOptions($options); $this->_objectCopyService->copyFieldsetToTarget('sales_convert_quote_item', 'to_order_item', $item, $orderItem); if ($item->getParentItem()) { $orderItem->setQtyOrdered($orderItem->getQtyOrdered() * $item->getParentItem()->getQty()); } if (!$item->getNoDiscount()) { $this->_objectCopyService->copyFieldsetToTarget('sales_convert_quote_item', 'to_order_item_discount', $item, $orderItem); } return $orderItem; }
/** * Save data from order to purchased links * * @param \Magento\Framework\Object $observer * @return $this */ public function saveDownloadableOrderItem($observer) { $orderItem = $observer->getEvent()->getItem(); if (!$orderItem->getId()) { //order not saved in the database return $this; } $product = $orderItem->getProduct(); if ($product && $product->getTypeId() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { return $this; } $purchasedLink = $this->_createPurchasedModel()->load($orderItem->getId(), 'order_item_id'); if ($purchasedLink->getId()) { return $this; } if (!$product) { $product = $this->_createProductModel()->setStoreId($orderItem->getOrder()->getStoreId())->load($orderItem->getProductId()); } if ($product->getTypeId() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { $links = $product->getTypeInstance()->getLinks($product); if ($linkIds = $orderItem->getProductOptionByCode('links')) { $linkPurchased = $this->_createPurchasedModel(); $this->_objectCopyService->copyFieldsetToTarget('downloadable_sales_copy_order', 'to_downloadable', $orderItem->getOrder(), $linkPurchased); $this->_objectCopyService->copyFieldsetToTarget('downloadable_sales_copy_order_item', 'to_downloadable', $orderItem, $linkPurchased); $linkSectionTitle = $product->getLinksTitle() ? $product->getLinksTitle() : $this->_scopeConfig->getValue(\Magento\Downloadable\Model\Link::XML_PATH_LINKS_TITLE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $linkPurchased->setLinkSectionTitle($linkSectionTitle)->save(); foreach ($linkIds as $linkId) { if (isset($links[$linkId])) { $linkPurchasedItem = $this->_createPurchasedItemModel()->setPurchasedId($linkPurchased->getId())->setOrderItemId($orderItem->getId()); $this->_objectCopyService->copyFieldsetToTarget('downloadable_sales_copy_link', 'to_purchased', $links[$linkId], $linkPurchasedItem); $linkHash = strtr(base64_encode(microtime() . $linkPurchased->getId() . $orderItem->getId() . $product->getId()), '+/=', '-_,'); $numberOfDownloads = $links[$linkId]->getNumberOfDownloads() * $orderItem->getQtyOrdered(); $linkPurchasedItem->setLinkHash($linkHash)->setNumberOfDownloadsBought($numberOfDownloads)->setStatus(\Magento\Downloadable\Model\Link\Purchased\Item::LINK_STATUS_PENDING)->setCreatedAt($orderItem->getCreatedAt())->setUpdatedAt($orderItem->getUpdatedAt())->save(); } } } } return $this; }
/** * Validate customer data and set some its data for further usage in quote * * Will return either true or array with error messages * * @param array $data * @return bool|array */ protected function _validateCustomerData(array $data) { $quote = $this->getQuote(); $isCustomerNew = !$quote->getCustomerId(); $customer = $quote->getCustomer(); $customerData = $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); /** @var Form $customerForm */ $customerForm = $this->_formFactory->create(\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'checkout_register', $customerData, $this->_request->isAjax(), Form::IGNORE_INVISIBLE, []); if ($isCustomerNew) { $customerRequest = $customerForm->prepareRequest($data); $customerData = $customerForm->extractData($customerRequest); } $customerErrors = $customerForm->validateData($customerData); if ($customerErrors !== true) { return ['error' => -1, 'message' => implode(', ', $customerErrors)]; } if (!$isCustomerNew) { return true; } $customer = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $customerData, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); if ($quote->getCheckoutMethod() == self::METHOD_REGISTER) { // We always have $customerRequest here, otherwise we would have been kicked off the function several // lines above $password = $customerRequest->getParam('customer_password'); if ($password != $customerRequest->getParam('confirm_password')) { return ['error' => -1, 'message' => __('Password and password confirmation are not equal.')]; } $quote->setPasswordHash($this->accountManagement->getPasswordHash($password)); } else { // set NOT LOGGED IN group id explicitly, // otherwise copyFieldsetToTarget('customer_account', 'to_quote') will fill it with default group id value $customer->setGroupId(GroupInterface::NOT_LOGGED_IN_ID); } //validate customer $result = $this->accountManagement->validate($customer); if (!$result->isValid()) { return ['error' => -1, 'message' => implode(', ', $result->getMessages())]; } // copy customer/guest email to address $quote->getBillingAddress()->setEmail($customer->getEmail()); // copy customer data to quote $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'), $quote); return true; }
/** * Define customer object * * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @return $this */ public function setCustomer(\Magento\Customer\Api\Data\CustomerInterface $customer = null) { /* @TODO: Remove the method after all external usages are refactored in MAGETWO-19930 */ $this->_customer = $customer; $this->setCustomerId($customer->getId()); $origAddresses = $customer->getAddresses(); $customer->setAddresses([]); $customerDataFlatArray = $this->objectFactory->create($this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface')); $customer->setAddresses($origAddresses); $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $customerDataFlatArray, $this); return $this; }
/** * Define customer object * * @param \Magento\Customer\Model\Customer $customer * @return $this */ public function setCustomer(\Magento\Customer\Model\Customer $customer) { /* @TODO: Remove the method after all external usages are refactored in MAGETWO-19930 */ $this->_customer = $customer; $this->setCustomerId($customer->getId()); $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $customer, $this); return $this; }
/** * Copy shipping address from order * * @param \Magento\Sales\Model\Order $order * @return void */ protected function _initShippingAddressFromOrder(\Magento\Sales\Model\Order $order) { $orderShippingAddress = $order->getShippingAddress(); $quoteShippingAddress = $this->getQuote()->getShippingAddress()->setCustomerAddressId('')->setSameAsBilling($orderShippingAddress && $orderShippingAddress->getSameAsBilling()); $this->_objectCopyService->copyFieldsetToTarget('sales_copy_order_shipping_address', 'to_order', $orderShippingAddress, $quoteShippingAddress); }