/** * Validates the fields in a specified address data object. * * @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object. * @return bool * @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer. * @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid. */ public function validate(\Magento\Quote\Api\Data\AddressInterface $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 %1', $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 %1', $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 %1 belongs to another customer', $addressData->getId())); } } } return true; }
/** * Return Sales Quote Address model (shipping address) * * @return \Magento\Quote\Model\Quote\Address */ public function getAddress() { if ($this->_address === null) { if ($this->isCustomerLoggedIn()) { $this->_address = $this->getQuote()->getShippingAddress(); } else { $this->_address = $this->_addressFactory->create(); } } return $this->_address; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->quoteAddressFactoryMock = $this->getMock('Magento\\Quote\\Model\\Quote\\AddressFactory', ['create'], [], '', false); $this->quoteAddressMock = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', ['isDeleted', 'getCollection', 'getId', 'getCustomerAddressId', '__wakeup', 'getAddressType', 'getDeleteImmediately', 'validateMinimumAmount', 'setData'], [], '', false); $this->quoteAddressCollectionMock = $this->getMock('Magento\\Quote\\Model\\Resource\\Quote\\Address\\Collection', [], [], '', false); $this->extensibleDataObjectConverterMock = $this->getMock('Magento\\Framework\\Api\\ExtensibleDataObjectConverter', ['toFlatArray'], [], '', false); $this->customerRepositoryMock = $this->getMockForAbstractClass('Magento\\Customer\\Api\\CustomerRepositoryInterface', [], '', false, true, true, ['getById']); $this->objectCopyServiceMock = $this->getMock('Magento\\Framework\\Object\\Copy', ['copyFieldsetToTarget'], [], '', 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\\Quote\\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->groupRepositoryMock = $this->getMockBuilder('Magento\\Customer\\Api\\GroupRepositoryInterface')->disableOriginalConstructor()->getMock(); $this->contextMock->expects($this->any())->method('getEventDispatcher')->will($this->returnValue($this->eventManagerMock)); $this->quoteItemCollectionFactoryMock = $this->getMock('Magento\\Quote\\Model\\Resource\\Quote\\Item\\CollectionFactory', ['create'], [], '', false); $this->quotePaymentCollectionFactoryMock = $this->getMock('Magento\\Quote\\Model\\Resource\\Quote\\Payment\\CollectionFactory', ['create'], [], '', false); $this->paymentFactoryMock = $this->getMock('Magento\\Quote\\Model\\Quote\\PaymentFactory', ['create'], [], '', false); $this->scopeConfig = $this->getMockBuilder('Magento\\Framework\\App\\Config')->disableOriginalConstructor()->getMock(); $this->addressRepositoryMock = $this->getMockForAbstractClass('Magento\\Customer\\Api\\AddressRepositoryInterface', [], '', false); $this->criteriaBuilderMock = $this->getMockBuilder('Magento\\Framework\\Api\\SearchCriteriaBuilder')->disableOriginalConstructor()->getMock(); $this->filterBuilderMock = $this->getMockBuilder('Magento\\Framework\\Api\\FilterBuilder')->disableOriginalConstructor()->getMock(); $this->extensionAttributesJoinProcessorMock = $this->getMock('Magento\\Framework\\Api\\ExtensionAttribute\\JoinProcessorInterface', [], [], '', false); $this->quote = (new ObjectManager($this))->getObject('Magento\\Quote\\Model\\Quote', ['quoteAddressFactory' => $this->quoteAddressFactoryMock, 'storeManager' => $this->storeManagerMock, 'resource' => $this->resourceMock, 'context' => $this->contextMock, 'customerFactory' => $this->customerFactoryMock, 'groupRepository' => $this->groupRepositoryMock, 'objectFactory' => $this->objectFactoryMock, 'addressRepository' => $this->addressRepositoryMock, 'criteriaBuilder' => $this->criteriaBuilderMock, 'filterBuilder' => $this->filterBuilderMock, 'quoteItemCollectionFactory' => $this->quoteItemCollectionFactoryMock, 'quotePaymentCollectionFactory' => $this->quotePaymentCollectionFactoryMock, 'quotePaymentFactory' => $this->paymentFactoryMock, 'scopeConfig' => $this->scopeConfig, 'extensibleDataObjectConverter' => $this->extensibleDataObjectConverterMock, 'customerRepository' => $this->customerRepositoryMock, 'objectCopyService' => $this->objectCopyServiceMock, 'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock]); }
/** * Update Customer name in Quote */ protected function _updateQuoteCustomerName() { /** @var $emptyAddress \Magento\Quote\Model\Quote\Address */ $emptyAddress = $this->_quoteAddressFactory->create(); $emptyAddress->setFirstname(null); $emptyAddress->setLastname(null); $this->_block->getQuote()->setBillingAddress($emptyAddress); $customer = $this->_customerRepository->getById(self::FIXTURE_CUSTOMER_ID); $customer->setFirstname(self::SAMPLE_FIRST_NAME)->setLastname(self::SAMPLE_LAST_NAME); $this->_block->getQuote()->setCustomer($customer); $this->_block->getQuote()->save(); $this->assertEquals(self::SAMPLE_FIRST_NAME, $this->_block->getFirstname()); $this->assertEquals(self::SAMPLE_LAST_NAME, $this->_block->getLastname()); }
/** * Return Sales Quote Address model * * @return \Magento\Quote\Model\Quote\Address */ public function getAddress() { if ($this->_address === null) { if ($this->isCustomerLoggedIn()) { $this->_address = $this->getQuote()->getBillingAddress(); if (!$this->_address->getFirstname()) { $this->_address->setFirstname($this->getQuote()->getCustomer()->getFirstname()); } if (!$this->_address->getLastname()) { $this->_address->setLastname($this->getQuote()->getCustomer()->getLastname()); } } else { $this->_address = $this->_addressFactory->create(); } } return $this->_address; }
/** * Add quote item to specific shipping address based on customer address id * * @param int $quoteItemId * @param array $data array('qty'=>$qty, 'address'=>$customerAddressId) * @throws \Magento\Framework\Exception\LocalizedException * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ 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) { if (!$this->isAddressIdApplicable($addressId)) { throw new LocalizedException(__('Please check shipping address information.')); } /** * Skip item processing if qty 0 */ if ($qty === 0) { return $this; } $quoteItem->setMultishippingQty((int) $quoteItem->getMultishippingQty() + $qty); $quoteItem->setQty($quoteItem->getMultishippingQty()); try { $address = $this->addressRepository->getById($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()); $quoteAddress->setCustomerAddressId($addressId); $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; }
/** * 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; }
/** * {@inheritdoc} */ public function createEmptyCart() { $storeId = $this->storeManager->getStore()->getStoreId(); $quote = $this->createAnonymousCart($storeId); $quote->setBillingAddress($this->quoteAddressFactory->create()); $quote->setShippingAddress($this->quoteAddressFactory->create()); try { $this->quoteRepository->save($quote); } catch (\Exception $e) { throw new CouldNotSaveException(__('Cannot create quote')); } return $quote->getId(); }