Exemple #1
0
 /**
  * Validate data object fields
  *
  * @param \Magento\Checkout\Service\V1\Data\Cart\Address $addressData
  * @return bool
  * @throws \Magento\Framework\Exception\InputException
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  */
 public function validate($addressData)
 {
     //validate customer id
     if ($addressData->getCustomerId()) {
         $customer = $this->customerFactory->create();
         $customer->load($addressData->getCustomerId());
         if (!$customer->getId()) {
             throw new \Magento\Framework\Exception\NoSuchEntityException('Invalid customer id ' . $addressData->getCustomerId());
         }
     }
     // validate address id
     if ($addressData->getId()) {
         $address = $this->quoteAddressFactory->create();
         $address->load($addressData->getId());
         if (!$address->getId()) {
             throw new \Magento\Framework\Exception\NoSuchEntityException('Invalid address id ' . $addressData->getId());
         }
         // check correspondence between customer id and address id
         if ($addressData->getCustomerId()) {
             if ($address->getCustomerId() != $addressData->getCustomerId()) {
                 throw new \Magento\Framework\Exception\InputException('Address with id ' . $addressData->getId() . ' belongs to another customer');
             }
         }
     }
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function setAddress($cartId, $addressData)
 {
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $this->storeManager->getStore()->getId());
     if ($quote->isVirtual()) {
         throw new NoSuchEntityException('Cart contains virtual product(s) only. Shipping address is not applicable');
     }
     /** @var \Magento\Sales\Model\Quote\Address $address */
     $address = $this->quoteAddressFactory->create();
     $this->addressValidator->validate($addressData);
     if ($addressData->getId()) {
         $address->load($addressData->getId());
     }
     $address = $this->addressConverter->convertDataObjectToModel($addressData, $address);
     $address->setSameAsBilling(0);
     $quote->setShippingAddress($address);
     $quote->setDataChanges(true);
     try {
         $quote->save();
     } catch (\Exception $e) {
         $this->logger->logException($e);
         throw new InputException('Unable to save address. Please, check input data.');
     }
     return $quote->getShippingAddress()->getId();
 }
Exemple #3
0
 /**
  * Return Sales Quote Address model (shipping address)
  *
  * @return \Magento\Sales\Model\Quote\Address
  */
 public function getAddress()
 {
     if (is_null($this->_address)) {
         if ($this->isCustomerLoggedIn()) {
             $this->_address = $this->getQuote()->getShippingAddress();
         } else {
             $this->_address = $this->_addressFactory->create();
         }
     }
     return $this->_address;
 }
 /**
  * {@inheritdoc}
  */
 public function setAddress($cartId, $addressData)
 {
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteRepository->get($cartId);
     /** @var \Magento\Sales\Model\Quote\Address $address */
     $address = $this->quoteAddressFactory->create();
     $this->addressValidator->validate($addressData);
     if ($addressData->getId()) {
         $address->load($addressData->getId());
     }
     $address = $this->addressConverter->convertDataObjectToModel($addressData, $address);
     $quote->setBillingAddress($address);
     $quote->setDataChanges(true);
     try {
         $quote->save();
     } catch (\Exception $e) {
         $this->logger->logException($e);
         throw new InputException('Unable to save address. Please, check input data.');
     }
     return $quote->getBillingAddress()->getId();
 }
Exemple #5
0
 /**
  * Update Customer name in Quote
  */
 protected function _updateQuoteCustomerName()
 {
     /** @var $emptyAddress \Magento\Sales\Model\Quote\Address */
     $emptyAddress = $this->_quoteAddressFactory->create();
     $emptyAddress->setFirstname(null);
     $emptyAddress->setLastname(null);
     $this->_block->getQuote()->setBillingAddress($emptyAddress);
     $customerData = $this->_customerService->getCustomer(self::FIXTURE_CUSTOMER_ID);
     $customerData = $this->_customerBuilder->populate($customerData)->setFirstname(self::SAMPLE_FIRST_NAME)->setLastname(self::SAMPLE_LAST_NAME)->create();
     $this->_block->getQuote()->setCustomerData($customerData);
     $this->_block->getQuote()->save();
     $this->assertEquals(self::SAMPLE_FIRST_NAME, $this->_block->getFirstname());
     $this->assertEquals(self::SAMPLE_LAST_NAME, $this->_block->getLastname());
 }
Exemple #6
0
 /**
  * Return Sales Quote Address model
  *
  * @return \Magento\Sales\Model\Quote\Address
  */
 public function getAddress()
 {
     if (is_null($this->_address)) {
         if ($this->isCustomerLoggedIn()) {
             $this->_address = $this->getQuote()->getBillingAddress();
             if (!$this->_address->getFirstname()) {
                 $this->_address->setFirstname($this->getQuote()->getCustomerData()->getFirstname());
             }
             if (!$this->_address->getLastname()) {
                 $this->_address->setLastname($this->getQuote()->getCustomerData()->getLastname());
             }
         } else {
             $this->_address = $this->_addressFactory->create();
         }
     }
     return $this->_address;
 }
Exemple #7
0
 protected function setUp()
 {
     $this->quoteAddressFactoryMock = $this->getMock('Magento\\Sales\\Model\\Quote\\AddressFactory', array('create'), array(), '', false);
     $this->quoteAddressMock = $this->getMock('Magento\\Sales\\Model\\Quote\\Address', ['isDeleted', 'getCollection', 'getId', 'getCustomerAddressId', '__wakeup', 'getAddressType', 'getDeleteImmediately'], [], '', false);
     $this->quoteAddressCollectionMock = $this->getMock('Magento\\Sales\\Model\\Resource\\Quote\\Address\\Collection', array(), array(), '', false);
     $this->productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', [], [], '', false);
     $this->objectFactoryMock = $this->getMock('\\Magento\\Framework\\Object\\Factory', ['create'], [], '', false);
     $this->quoteAddressFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->quoteAddressMock));
     $this->quoteAddressMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->quoteAddressCollectionMock));
     $this->eventManagerMock = $this->getMockBuilder('Magento\\Framework\\Event\\Manager')->disableOriginalConstructor()->getMock();
     $this->storeManagerMock = $this->getMockBuilder('Magento\\Store\\Model\\StoreManager')->disableOriginalConstructor()->getMock();
     $this->resourceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Resource\\Quote')->disableOriginalConstructor()->getMock();
     $this->contextMock = $this->getMockBuilder('Magento\\Framework\\Model\\Context')->disableOriginalConstructor()->getMock();
     $this->customerFactoryMock = $this->getMockBuilder('Magento\\Customer\\Model\\CustomerFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock();
     $this->addressConverterMock = $this->getMockBuilder('Magento\\Customer\\Model\\Address\\Converter')->disableOriginalConstructor()->getMock();
     $this->customerGroupServiceMock = $this->getMockBuilder('Magento\\Customer\\Service\\V1\\CustomerGroupService')->disableOriginalConstructor()->getMock();
     $this->contextMock->expects($this->any())->method('getEventDispatcher')->will($this->returnValue($this->eventManagerMock));
     $this->quoteItemCollectionFactoryMock = $this->getMock('Magento\\Sales\\Model\\Resource\\Quote\\Item\\CollectionFactory', ['create'], [], '', false);
     $this->quote = (new ObjectManager($this))->getObject('Magento\\Sales\\Model\\Quote', ['quoteAddressFactory' => $this->quoteAddressFactoryMock, 'storeManager' => $this->storeManagerMock, 'resource' => $this->resourceMock, 'context' => $this->contextMock, 'customerFactory' => $this->customerFactoryMock, 'addressConverter' => $this->addressConverterMock, 'customerGroupService' => $this->customerGroupServiceMock, 'objectFactory' => $this->objectFactoryMock, 'quoteItemCollectionFactory' => $this->quoteItemCollectionFactoryMock]);
 }
 /**
  * Add quote item to specific shipping address based on customer address id
  *
  * @param int $quoteItemId
  * @param array $data array('qty'=>$qty, 'address'=>$customerAddressId)
  * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping
  */
 protected function _addShippingItem($quoteItemId, $data)
 {
     $qty = isset($data['qty']) ? (int) $data['qty'] : 1;
     //$qty       = $qty > 0 ? $qty : 1;
     $addressId = isset($data['address']) ? $data['address'] : false;
     $quoteItem = $this->getQuote()->getItemById($quoteItemId);
     if ($addressId && $quoteItem) {
         /**
          * Skip item processing if qty 0
          */
         if ($qty === 0) {
             return $this;
         }
         $quoteItem->setMultishippingQty((int) $quoteItem->getMultishippingQty() + $qty);
         $quoteItem->setQty($quoteItem->getMultishippingQty());
         try {
             $address = $this->_customerAddressService->getAddress($addressId);
         } catch (\Exception $e) {
         }
         if (isset($address)) {
             if (!($quoteAddress = $this->getQuote()->getShippingAddressByCustomerAddressId($address->getId()))) {
                 $quoteAddress = $this->_addressFactory->create()->importCustomerAddressData($address);
                 $this->getQuote()->addShippingAddress($quoteAddress);
             }
             $quoteAddress = $this->getQuote()->getShippingAddressByCustomerAddressId($address->getId());
             $quoteAddressItem = $quoteAddress->getItemByQuoteItemId($quoteItemId);
             if ($quoteAddressItem) {
                 $quoteAddressItem->setQty((int) ($quoteAddressItem->getQty() + $qty));
             } else {
                 $quoteAddress->addItem($quoteItem, $qty);
             }
             /**
              * Require shipping rate recollect
              */
             $quoteAddress->setCollectShippingRates((bool) $this->getCollectRatesFlag());
         }
     }
     return $this;
 }
Exemple #9
0
 /**
  * Retrieve quote address by type
  *
  * @param   string $type
  * @return  Address
  */
 protected function _getAddressByType($type)
 {
     foreach ($this->getAddressesCollection() as $address) {
         if ($address->getAddressType() == $type && !$address->isDeleted()) {
             return $address;
         }
     }
     $address = $this->_quoteAddressFactory->create()->setAddressType($type);
     $this->addAddress($address);
     return $address;
 }
Exemple #10
0
 /**
  * Convert order address to quote address
  *
  * @param   \Magento\Sales\Model\Order\Address $address
  * @return  \Magento\Sales\Model\Quote\Address
  */
 public function addressToQuoteAddress(\Magento\Sales\Model\Order\Address $address)
 {
     $quoteAddress = $this->_quoteAddressFactory->create()->setStoreId($address->getStoreId())->setAddressType($address->getAddressType())->setCustomerId($address->getCustomerId())->setCustomerAddressId($address->getCustomerAddressId());
     $this->_objectCopyService->copyFieldsetToTarget('sales_convert_order_address', 'to_quote_address', $address, $quoteAddress);
     return $quoteAddress;
 }