/** * Adds address to address book. * * @param UcAddressesAddress $address * (optional) An instance of UcAddressesAddress to add. * Defaults to a new instance of UcAddressesAddress. * * @access public * @return UcAddressesAddress * The instance of UcAddressesAddress that was added. * @throws UcAddressesInvalidParameterException * @throws UcAddressesNameCollisionException */ public function addAddress(UcAddressesAddress $address = NULL) { // If we add an address, then we'll probably save it, which // requires loading all addresses for error checking. if ($this->performanceHint == self::PERF_HINT_LOAD_ONE) { $this->performanceHint = self::PERF_HINT_LOAD_ALL; } if ($address) { // In case of a new address with an address name, // load other addresses to do a name check comparison. if ($address->isNew() && $address->getName() && !$this->allLoaded) { $this->loadAll(); } // Check if address is already in addressbook. foreach ($this->addresses as $aid => $addressBookAddress) { if ($address === $addressBookAddress) { throw new UcAddressesInvalidParameterException(t('Tried to add an address already in the address book')); } if ($address->getName() && $address->getName() == $addressBookAddress->getName()) { throw new UcAddressesNameCollisionException(t('Tried to add an address with a name matching that of an address already in the address book')); } } // Check if address belongs to this address book. if ($address->getAddressBook() !== $this && $address->isOwned()) { throw new UcAddressesInvalidParameterException(t('Tried to add an address already in an other address book')); } } if (!$address) { $address = new UcAddressesAddress($this); } $this->addresses[$address->getId()] = $address; if ($address->isDefault('shipping')) { $this->defaultAddresses['shipping'] = $address; } if ($address->isDefault('billing')) { $this->defaultAddresses['billing'] = $address; } // Make sure this becomes one of our addresses. if ($address->getAddressBook() !== $this) { $address->privChangeAddressBook($this); } return $address; }
/** * This hook allows you alter the address just before it's saved. * * @param UcAddressesAddress $address * The address object. * * @return void */ function hook_uc_addresses_address_presave($address) { // Example: set a nickname for this address if there is none. if ($address->getName() == '') { $nickname = 'my address name'; if (!$address->setName($nickname)) { // Try an other name if this nickname is already used. $numb = 2; $other_nickname = $nickname . ' ' . $numb++; while (!$address->setName($other_nickname)) { $other_nickname = $nickname . ' ' . $numb++; } } } }