/** * 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(); }
/** * 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; }
/** * {@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; }