/** * Save customer address. * * @param \Magento\Customer\Api\Data\AddressInterface $address * @return \Magento\Customer\Api\Data\AddressInterface * @throws \Magento\Framework\Exception\LocalizedException */ public function save(\Magento\Customer\Api\Data\AddressInterface $address) { $addressModel = null; $customerModel = $this->customerRegistry->retrieve($address->getCustomerId()); if ($address->getId()) { $addressModel = $this->addressRegistry->retrieve($address->getId()); } if ($addressModel === null) { $addressModel = $this->addressFactory->create(); $addressModel->updateData($address); $addressModel->setCustomer($customerModel); } else { $addressModel->updateData($address); } $inputException = $this->_validate($addressModel); if ($inputException->wasErrorAdded()) { throw $inputException; } $addressModel->save(); // Clean up the customer registry since the Address save has a // side effect on customer : \Magento\Customer\Model\Resource\Address::_afterSave $this->customerRegistry->remove($address->getCustomerId()); $this->addressRegistry->push($addressModel); $customerModel->getAddressesCollection()->clear(); return $addressModel->getDataModel(); }
public function testRemove() { $addressId = 1; $address = $this->getMockBuilder('\\Magento\\Customer\\Model\\Address')->disableOriginalConstructor()->setMethods(['load', 'getId', '__wakeup'])->getMock(); $address->expects($this->exactly(2))->method('load')->with($addressId)->will($this->returnValue($address)); $address->expects($this->exactly(2))->method('getId')->will($this->returnValue($addressId)); $this->addressFactory->expects($this->exactly(2))->method('create')->will($this->returnValue($address)); $actual = $this->unit->retrieve($addressId); $this->assertEquals($address, $actual); $this->unit->remove($addressId); $actual = $this->unit->retrieve($addressId); $this->assertEquals($address, $actual); }
/** * Get instance of the Address Model identified by id * * @param int $addressId * @return Address * @throws NoSuchEntityException */ public function retrieve($addressId) { if (isset($this->registry[$addressId])) { return $this->registry[$addressId]; } $address = $this->addressFactory->create(); $address->load($addressId); if (!$address->getId()) { throw NoSuchEntityException::singleField('addressId', $addressId); } $this->registry[$addressId] = $address; return $address; }
public function testSave() { $customerId = 34; $addressId = 53; $customerAddress = $this->getMockForAbstractClass('Magento\\Customer\\Api\\Data\\AddressInterface', [], '', false); $addressCollection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Address\\Collection', [], [], '', false); $customerAddress->expects($this->atLeastOnce())->method('getCustomerId')->willReturn($customerId); $customerAddress->expects($this->atLeastOnce())->method('getId')->willReturn($addressId); $this->customerRegistry->expects($this->once())->method('retrieve')->with($customerId)->willReturn($this->customer); $this->addressRegistry->expects($this->once())->method('retrieve')->with($addressId)->willReturn(null); $this->addressFactory->expects($this->once())->method('create')->willReturn($this->address); $this->address->expects($this->once())->method('updateData')->with($customerAddress); $this->address->expects($this->once())->method('setCustomer')->with($this->customer); $this->address->expects($this->once())->method('save'); $this->customerRegistry->expects($this->once())->method('remove')->with($customerId); $this->addressRegistry->expects($this->once())->method('push')->with($this->address); $this->customer->expects($this->once())->method('getAddressesCollection')->willReturn($addressCollection); $addressCollection->expects($this->once())->method('clear'); $this->address->expects($this->once())->method('getDataModel')->willReturn($customerAddress); $this->address->expects($this->once())->method('getShouldIgnoreValidation')->willReturn(true); $this->repository->save($customerAddress); }
/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing Gift Registry:'); $fixtureFile = 'GiftRegistry/gift_registry.csv'; $fixtureFilePath = $this->fixtureHelper->getPath($fixtureFile); /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */ $csvReader = $this->csvReaderFactory->create(['fileName' => $fixtureFilePath, 'mode' => 'r']); foreach ($csvReader as $giftRegistryData) { /** @var \Magento\GiftRegistry\Model\Resource\Entity\Collection $collection */ $collection = $this->collectionFactory->create(); $collection->addFilter('title', $giftRegistryData['title']); if ($collection->count() > 0) { continue; } $data = $this->generateData($giftRegistryData); /** @var \Magento\GiftRegistry\Model\Entity $giftRegistry */ $giftRegistry = $this->giftRegistryFactory->create(); $address = $this->addressFactory->create(); $address->setData($data['address']); $giftRegistry->setTypeById($data['type_id']); $giftRegistry->importData($data); $giftRegistry->addData(['customer_id' => $data['customer_id'], 'website_id' => $this->storeManager->getWebsiteId(), 'url_key' => $giftRegistry->getGenerateKeyId(), 'created_at' => $this->dateFactory->create()->date(), 'is_add_action' => true]); $giftRegistry->importAddress($address); $validationPassed = $giftRegistry->validate(); if ($validationPassed) { $giftRegistry->save(); foreach ($data['items'] as $productId) { $parentId = $this->productIndexer->getRelationsByChild($productId); $itemProduct = $parentId ? $parentId[0] : $productId; $itemOptions = $this->formItemOptions($productId); $item = $this->itemFactory->create(); $item->setEntityId($giftRegistry->getId())->setProductId($itemProduct)->setQty(1)->setOptions($itemOptions)->save(); } } $this->logger->logInline('.'); } }
/** * Get next address entity ID * * @return int */ protected function _getNextEntityId() { if (!$this->_nextEntityId) { /** @var $addressResource \Magento\Customer\Model\Resource\Address */ $addressResource = $this->_addressFactory->create()->getResource(); $addressTable = $addressResource->getEntityTable(); $this->_nextEntityId = $this->_resourceHelper->getNextAutoincrement($addressTable); } return $this->_nextEntityId++; }
/** * @return Address */ protected function _createAddressInstance() { return $this->_addressFactory->create(); }
/** * Creates an address model out of an address Data Object. * * @param Address $addressDataObject * @return AddressModel */ public function createAddressModel(Address $addressDataObject) { $addressModel = $this->_addressFactory->create(); $this->updateAddressModel($addressModel, $addressDataObject); return $addressModel; }