/** * @param Address $object * @param array $data * @return OrderInterface */ public function convert(Address $object, $data = []) { $orderData = $this->objectCopyService->getDataFromFieldset( 'quote_convert_address', 'to_order', $object ); /** * @var $order \Magento\Sales\Model\Order */ $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()) ->setIncrementId($object->getQuote()->getReservedOrderId()); $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; }
/** * 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; }
public function testCopyFieldsetArrayTarget() { $fieldset = 'sales_copy_order'; $aspect = 'to_edit'; $data = ['customer_email' => '*****@*****.**', 'customer_group_id' => '1']; $source = new \Magento\Framework\DataObject($data); $target = []; $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 testGetCopyFieldsetToTargetWhenTargetIsAbstractSimpleObject() { $fields['code']['aspect'] = '*'; $source['code'] = 'code'; $this->fieldsetConfigMock->expects($this->once())->method('getFieldset')->with('fieldset', 'global')->will($this->returnValue($fields)); $sourceMock = $this->getMock('Magento\\Framework\\Api\\AbstractSimpleObject', ['__toArray'], [], '', false); $targetMock = $this->getMock('Magento\\Framework\\Api\\AbstractSimpleObject', ['setData'], [], '', false); $sourceMock->expects($this->once())->method('__toArray')->willReturn($source); $targetMock->expects($this->once())->method('setData')->with('code', 'code'); $this->eventManagerMock->expects($this->once())->method('dispatch'); $result = $this->copy->copyFieldsetToTarget('fieldset', 'aspect', $sourceMock, $targetMock); $this->assertEquals($result, $targetMock); }
/** * {@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]); switch ($address->getAddressType()) { case QuoteAddress::ADDRESS_TYPE_BILLING: $customerAddress->setIsDefaultBilling(true); break; case QuoteAddress::ADDRESS_TYPE_SHIPPING: $customerAddress->setIsDefaultShipping(true); break; } 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; }
/** * Save data from order to purchased links * * @param \Magento\Framework\DataObject $observer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute(\Magento\Framework\Event\Observer $observer) { $orderItem = $observer->getEvent()->getItem(); if (!$orderItem->getId()) { //order not saved in the database return $this; } if ($orderItem->getProductType() != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { return $this; } $product = $orderItem->getProduct(); $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, 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; }
/** * 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); }
/** * 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; }
/** * 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; }