1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Customer_Model_Address_Api_V2 extends Mage_Customer_Model_Address_Api
35: {
36: 37: 38: 39: 40: 41: 42:
43: public function create($customerId, $addressData)
44: {
45: $customer = Mage::getModel('customer/customer')
46: ->load($customerId);
47:
48:
49: if (!$customer->getId()) {
50: $this->_fault('customer_not_exists');
51: }
52:
53: $address = Mage::getModel('customer/address');
54:
55: foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
56: if (isset($addressData->$attributeCode)) {
57: $address->setData($attributeCode, $addressData->$attributeCode);
58: }
59: }
60:
61: if (isset($addressData->is_default_billing)) {
62: $address->setIsDefaultBilling($addressData->is_default_billing);
63: }
64:
65: if (isset($addressData->is_default_shipping)) {
66: $address->setIsDefaultShipping($addressData->is_default_shipping);
67: }
68:
69: $address->setCustomerId($customer->getId());
70:
71: $valid = $address->validate();
72:
73: if (is_array($valid)) {
74: $this->_fault('data_invalid', implode("\n", $valid));
75: }
76:
77: try {
78: $address->save();
79: } catch (Mage_Core_Exception $e) {
80: $this->_fault('data_invalid', $e->getMessage());
81: }
82:
83: return $address->getId();
84: }
85:
86: 87: 88: 89: 90: 91:
92: public function info($addressId)
93: {
94: $address = Mage::getModel('customer/address')
95: ->load($addressId);
96:
97: if (!$address->getId()) {
98: $this->_fault('not_exists');
99: }
100:
101: $result = array();
102:
103: foreach ($this->_mapAttributes as $attributeAlias => $attributeCode) {
104: $result[$attributeAlias] = $address->getData($attributeCode);
105: }
106:
107: foreach ($this->getAllowedAttributes($address) as $attributeCode => $attribute) {
108: $result[$attributeCode] = $address->getData($attributeCode);
109: }
110:
111:
112: if ($customer = $address->getCustomer()) {
113: $result['is_default_billing'] = $customer->getDefaultBilling() == $address->getId();
114: $result['is_default_shipping'] = $customer->getDefaultShipping() == $address->getId();
115: }
116:
117: return $result;
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: public function update($addressId, $addressData)
128: {
129: $address = Mage::getModel('customer/address')
130: ->load($addressId);
131:
132: if (!$address->getId()) {
133: $this->_fault('not_exists');
134: }
135:
136: foreach ($this->getAllowedAttributes($address) as $attributeCode=>$attribute) {
137: if (isset($addressData->$attributeCode)) {
138: $address->setData($attributeCode, $addressData->$attributeCode);
139: }
140: }
141:
142: if (isset($addressData->is_default_billing)) {
143: $address->setIsDefaultBilling($addressData->is_default_billing);
144: }
145:
146: if (isset($addressData->is_default_shipping)) {
147: $address->setIsDefaultShipping($addressData->is_default_shipping);
148: }
149:
150: $valid = $address->validate();
151: if (is_array($valid)) {
152: $this->_fault('data_invalid', implode("\n", $valid));
153: }
154:
155: try {
156: $address->save();
157: } catch (Mage_Core_Exception $e) {
158: $this->_fault('data_invalid', $e->getMessage());
159: }
160:
161: return true;
162: }
163:
164: 165: 166: 167: 168: 169:
170: public function delete($addressId)
171: {
172: $address = Mage::getModel('customer/address')
173: ->load($addressId);
174:
175: if (!$address->getId()) {
176: $this->_fault('not_exists');
177: }
178:
179: try {
180: $address->delete();
181: } catch (Mage_Core_Exception $e) {
182: $this->_fault('not_deleted', $e->getMessage());
183: }
184:
185: return true;
186: }
187: }
188: