00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Customer_Model_Address_Api_V2 extends Mage_Customer_Model_Address_Api
00035 {
00036
00037
00038
00039
00040
00041
00042
00043 public function create($customerId, $addressData)
00044 {
00045 $customer = Mage::getModel('customer/customer')
00046 ->load($customerId);
00047
00048
00049 if (!$customer->getId()) {
00050 $this->_fault('customer_not_exists');
00051 }
00052
00053 $address = Mage::getModel('customer/address');
00054
00055 foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
00056 if (isset($addressData->$attributeCode)) {
00057 $address->setData($attributeCode, $addressData->$attributeCode);
00058 }
00059 }
00060
00061 if (isset($addressData->is_default_billing)) {
00062 $address->setIsDefaultBilling($addressData->is_default_billing);
00063 }
00064
00065 if (isset($addressData->is_default_shipping)) {
00066 $address->setIsDefaultShipping($addressData->is_default_shipping);
00067 }
00068
00069 $address->setCustomerId($customer->getId());
00070
00071 $valid = $address->validate();
00072
00073 if (is_array($valid)) {
00074 $this->_fault('data_invalid', implode("\n", $valid));
00075 }
00076
00077 try {
00078 $address->save();
00079 } catch (Mage_Core_Exception $e) {
00080 $this->_fault('data_invalid', $e->getMessage());
00081 }
00082
00083 return $address->getId();
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 public function info($addressId)
00093 {
00094 $address = Mage::getModel('customer/address')
00095 ->load($addressId);
00096
00097 if (!$address->getId()) {
00098 $this->_fault('not_exists');
00099 }
00100
00101 $result = array();
00102
00103 foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
00104 $result[$attributeAlias] = $address->getData($attributeCode);
00105 }
00106
00107 foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
00108 $result[$attributeCode] = $address->getData($attributeCode);
00109 }
00110
00111
00112 if ($customer = $address->getCustomer()) {
00113 $result['is_default_billing'] = $customer->getDefaultBillingAddress() == $address->getId();
00114 $result['is_default_shipping'] = $customer->getDefaultShippingAddress() == $address->getId();
00115 }
00116
00117 return $result;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127 public function update($addressId, $addressData)
00128 {
00129 $address = Mage::getModel('customer/address')
00130 ->load($addressId);
00131
00132 if (!$address->getId()) {
00133 $this->_fault('not_exists');
00134 }
00135
00136 foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
00137 if (isset($addressData->$attributeCode)) {
00138 $address->setData($attributeCode, $addressData->$attributeCode);
00139 }
00140 }
00141
00142 if (isset($addressData->is_default_billing)) {
00143 $address->setIsDefaultBilling($addressData->is_default_billing);
00144 }
00145
00146 if (isset($addressData->is_default_shipping)) {
00147 $address->setIsDefaultShipping($addressData->is_default_shipping);
00148 }
00149
00150 $valid = $address->validate();
00151 if (is_array($valid)) {
00152 $this->_fault('data_invalid', implode("\n", $valid));
00153 }
00154
00155 try {
00156 $address->save();
00157 } catch (Mage_Core_Exception $e) {
00158 $this->_fault('data_invalid', $e->getMessage());
00159 }
00160
00161 return true;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170 public function delete($addressId)
00171 {
00172 $address = Mage::getModel('customer/address')
00173 ->load($addressId);
00174
00175 if (!$address->getId()) {
00176 $this->_fault('not_exists');
00177 }
00178
00179 try {
00180 $address->delete();
00181 } catch (Mage_Core_Exception $e) {
00182 $this->_fault('not_deleted', $e->getMessage());
00183 }
00184
00185 return true;
00186 }
00187 }