Beispiel #1
0
 /**
  * @param \Magento\Quote\Model\Quote $quote
  * @return \Magento\Quote\Model\Quote
  */
 public function prepareQuoteForNewCustomer(\Magento\Quote\Model\Quote $quote)
 {
     $billing = $quote->getBillingAddress();
     $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress();
     $billing->setDefaultBilling(true);
     if ($shipping && !$shipping->getSameAsBilling()) {
         $shipping->setDefaultShipping(true);
         $address = $shipping->exportCustomerAddress();
         $shipping->setCustomerAddressData($address);
     } elseif ($shipping) {
         $billing->setDefaultShipping(true);
     }
     $address = $shipping->exportCustomerAddress();
     $billing->setCustomerAddressData($address);
     foreach (['customer_dob', 'customer_taxvat', 'customer_gender'] as $attribute) {
         if ($quote->getData($attribute) && !$billing->getData($attribute)) {
             $billing->setData($attribute, $quote->getData($attribute));
         }
     }
     $customer = $this->customerFactory->create();
     $this->dataObjectHelper->populateWithArray($customer, $this->copyObject->getDataFromFieldset('checkout_onepage_billing', 'to_customer', $billing), '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
     $customer->setEmail($quote->getCustomerEmail());
     $customer->setPrefix($quote->getCustomerPrefix());
     $customer->setFirstname($quote->getCustomerFirstname());
     $customer->setMiddlename($quote->getCustomerMiddlename());
     $customer->setLastname($quote->getCustomerLastname());
     $customer->setSuffix($quote->getCustomerSuffix());
     $quote->setCustomer($customer);
     $quote->addCustomerAddress($billing->exportCustomerAddress());
     if ($shipping->hasCustomerAddress()) {
         $quote->addCustomerAddress($shipping->exportCustomerAddress());
     }
     return $quote;
 }
 /**
  * @param Address $object
  * @param array $data
  * @return OrderAddressInterface
  */
 public function convert(Address $object, $data = [])
 {
     $orderAddressData = $this->objectCopyService->getDataFromFieldset('quote_convert_address', 'to_order_address', $object);
     $orderAddress = $this->orderAddressFactory->create();
     $this->dataObjectHelper->populateWithArray($orderAddress, array_merge($orderAddressData, $data), '\\Magento\\Sales\\Api\\Data\\OrderAddressInterface');
     return $orderAddress;
 }
Beispiel #3
0
 /**
  * {@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;
 }
Beispiel #4
0
 /**
  * @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;
 }
Beispiel #5
0
 /**
  * test for convert method
  */
 public function testConvert()
 {
     $this->quoteItemMock->expects($this->exactly(2))->method('getProduct')->willReturn($this->productMock);
     $this->productMock->expects($this->once())->method('getTypeInstance')->willReturn($this->productTypeMock);
     $this->productTypeMock->expects($this->once())->method('getOrderOptions')->with($this->productMock)->willReturn(['option']);
     $this->objectCopyServiceMock->expects($this->at(0))->method('getDataFromFieldset')->with('quote_convert_item', 'to_order_item', $this->quoteItemMock)->willReturn([]);
     $this->objectCopyServiceMock->expects($this->at(1))->method('getDataFromFieldset')->with('quote_convert_item', 'to_order_item_discount', $this->quoteItemMock)->willReturn([]);
     $this->orderItemBuilderMock->expects($this->once())->method('create')->willReturn($this->orderItemMock);
     $this->assertInstanceOf('Magento\\Sales\\Model\\Order\\Item', $this->converter->convert($this->quoteItemMock, []));
 }
Beispiel #6
0
 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));
 }
 /**
  * @param Payment $object
  * @param array $data
  * @return OrderPaymentInterface
  */
 public function convert(Payment $object, $data = [])
 {
     $paymentData = $this->objectCopyService->getDataFromFieldset('quote_convert_payment', 'to_order_payment', $object);
     $orderPayment = $this->orderPaymentFactory->create();
     $this->dataObjectHelper->populateWithArray($orderPayment, array_merge($paymentData, $data), '\\Magento\\Sales\\Api\\Data\\OrderPaymentInterface');
     $orderPayment->setAdditionalInformation(serialize(array_merge($object->getAdditionalInformation(), [Substitution::INFO_KEY_TITLE => $object->getMethodInstance()->getTitle()])));
     // set directly on the model
     $orderPayment->setCcNumber($object->getCcNumber());
     $orderPayment->setCcCid($object->getCcCid());
     return $orderPayment;
 }
Beispiel #8
0
 public function testPrepareQuoteForNewCustomer()
 {
     $customerAddressMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\AddressInterface')->disableOriginalConstructor()->getMock();
     $this->addressMock->expects($this->any())->method('exportCustomerAddress')->willReturn($customerAddressMock);
     $this->addressMock->expects($this->any())->method('getData')->willReturn([]);
     $this->addressFactoryMock->expects($this->any())->method('create')->willReturn($this->addressMock);
     $this->quoteMock->expects($this->any())->method('isVirtual')->willReturn(false);
     $this->quoteMock->expects($this->once())->method('getBillingAddress')->willReturn($this->addressMock);
     $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->addressMock);
     $customerDataMock = $this->getMockBuilder('Magento\\Customer\\Api\\Data\\CustomerInterface')->disableOriginalConstructor()->getMock();
     $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerDataMock);
     $this->copyObjectMock->expects($this->any())->method('getDataFromFieldset')->willReturn([]);
     $this->dataObjectHelper->expects($this->any())->method('populateWithArray')->willReturnSelf();
     $this->assertInstanceOf('Magento\\Quote\\Model\\Quote', $this->quote->prepareQuoteForNewCustomer($this->quoteMock));
 }
Beispiel #9
0
 public function testGetDataFromFieldsetWhenFieldDoesNotExists()
 {
     $fields['code']['aspect'] = [];
     $this->fieldsetConfigMock->expects($this->once())->method('getFieldset')->with('fieldset', 'global')->will($this->returnValue($fields));
     $this->sourceMock->expects($this->never())->method('getDataUsingMethod');
     $this->assertEquals([], $this->copy->getDataFromFieldset('fieldset', 'aspect', $this->sourceMock));
 }
Beispiel #10
0
 /**
  * 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;
 }
 /**
  * @param Item|AddressItem $item
  * @param array $data
  * @return OrderItemInterface
  */
 public function convert($item, $data = [])
 {
     $options = $item->getProductOrderOptions();
     if (!$options) {
         $options = $item->getProduct()->getTypeInstance()->getOrderOptions($item->getProduct());
     }
     $orderItemData = $this->objectCopyService->getDataFromFieldset('quote_convert_item', 'to_order_item', $item);
     if (!$item->getNoDiscount()) {
         $data = array_merge($data, $this->objectCopyService->getDataFromFieldset('quote_convert_item', 'to_order_item_discount', $item));
     }
     $orderItem = $this->orderItemFactory->create();
     $this->dataObjectHelper->populateWithArray($orderItem, array_merge($orderItemData, $data), '\\Magento\\Sales\\Api\\Data\\OrderItemInterface');
     $orderItem->setProductOptions($options);
     if ($item->getParentItem()) {
         $orderItem->setQtyOrdered($orderItemData[OrderItemInterface::QTY_ORDERED] * $item->getParentItem()->getQty());
     }
     return $orderItem;
 }
Beispiel #12
0
 /**
  * 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;
 }
Beispiel #13
0
 /**
  * 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;
 }
Beispiel #14
0
 /**
  * Prepare quote for customer registration and customer order submit
  *
  * @return void
  */
 protected function _prepareNewCustomerQuote()
 {
     $quote = $this->getQuote();
     $billing = $quote->getBillingAddress();
     $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress();
     $customer = $quote->getCustomer();
     $customerBillingData = $billing->exportCustomerAddress();
     $dataArray = $this->_objectCopyService->getDataFromFieldset('checkout_onepage_quote', 'to_customer', $quote);
     $this->dataObjectHelper->populateWithArray($customer, $dataArray, '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
     $quote->setCustomer($customer)->setCustomerId(true);
     $customerBillingData->setIsDefaultBilling(true);
     if ($shipping) {
         if (!$shipping->getSameAsBilling()) {
             $customerShippingData = $shipping->exportCustomerAddress();
             $customerShippingData->setIsDefaultShipping(true);
             $shipping->setCustomerAddressData($customerShippingData);
             // Add shipping address to quote since customer Data Object does not hold address information
             $quote->addCustomerAddress($customerShippingData);
         } else {
             $shipping->setCustomerAddressData($customerBillingData);
             $customerBillingData->setIsDefaultShipping(true);
         }
     } else {
         $customerBillingData->setIsDefaultShipping(true);
     }
     $billing->setCustomerAddressData($customerBillingData);
     // TODO : Eventually need to remove this legacy hack
     // Add billing address to quote since customer Data Object does not hold address information
     $quote->addCustomerAddress($customerBillingData);
 }
Beispiel #15
0
 /**
  * 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;
 }
Beispiel #16
0
 /**
  * Prepare quote for customer registration and customer order submit
  * and restore magento customer data from quote
  *
  * @return void
  */
 protected function _prepareNewCustomerQuote()
 {
     $quote = $this->_quote;
     $billing = $quote->getBillingAddress();
     $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress();
     /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $billingAddressBuilder */
     $billingAddressBuilder = $this->_addressBuilderFactory->create();
     $customerBilling = $billingAddressBuilder->populate($billing->exportCustomerAddressData())->setDefaultBilling(true);
     if ($shipping && !$shipping->getSameAsBilling()) {
         /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $shippingAddressBuilder */
         $shippingAddressBuilder = $this->_addressBuilderFactory->create();
         $customerShipping = $shippingAddressBuilder->populate($shipping->exportCustomerAddressData())->setDefaultShipping(true)->create();
         $shipping->setCustomerAddressData($customerShipping);
     } elseif ($shipping) {
         $customerBilling->setDefaultShipping(true);
     }
     $customerBilling = $customerBilling->create();
     $billing->setCustomerAddressData($customerBilling);
     /**
      * @todo integration with dynamic attributes customer_dob, customer_taxvat, customer_gender
      */
     if ($quote->getCustomerDob() && !$billing->getCustomerDob()) {
         $billing->setCustomerDob($quote->getCustomerDob());
     }
     if ($quote->getCustomerTaxvat() && !$billing->getCustomerTaxvat()) {
         $billing->setCustomerTaxvat($quote->getCustomerTaxvat());
     }
     if ($quote->getCustomerGender() && !$billing->getCustomerGender()) {
         $billing->setCustomerGender($quote->getCustomerGender());
     }
     $customerData = $this->_objectCopyService->getDataFromFieldset('checkout_onepage_billing', 'to_customer', $billing);
     $customer = $this->_customerBuilder->populateWithArray($customerData);
     $customer->setEmail($quote->getCustomerEmail());
     $customer->setPrefix($quote->getCustomerPrefix());
     $customer->setFirstname($quote->getCustomerFirstname());
     $customer->setMiddlename($quote->getCustomerMiddlename());
     $customer->setLastname($quote->getCustomerLastname());
     $customer->setSuffix($quote->getCustomerSuffix());
     $quote->setCustomerData($customer->create())->addCustomerAddressData($customerBilling);
     if (isset($customerShipping)) {
         $quote->addCustomerAddressData($customerShipping);
     }
 }
Beispiel #17
0
 /**
  * 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;
 }
Beispiel #18
0
 /**
  * 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);
 }