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_Checkout_Model_Cart_Customer_Api extends Mage_Checkout_Model_Api_Resource_Customer
35: {
36: public function __construct()
37: {
38: $this->_storeIdSessionField = "cart_store_id";
39:
40: $this->_attributesMap['quote'] = array('quote_id' => 'entity_id');
41: $this->_attributesMap['quote_customer'] = array('customer_id' => 'entity_id');
42: $this->_attributesMap['quote_address'] = array('address_id' => 'entity_id');
43: }
44:
45: 46: 47: 48: 49: 50: 51: 52:
53: public function set($quoteId, $customerData, $store = null)
54: {
55: $quote = $this->_getQuote($quoteId, $store);
56:
57: $customerData = $this->_prepareCustomerData($customerData);
58: if (!isset($customerData['mode'])) {
59: $this->_fault('customer_mode_is_unknown');
60: }
61:
62: switch($customerData['mode']) {
63: case self::MODE_CUSTOMER:
64:
65: $customer = $this->_getCustomer($customerData['entity_id']);
66: $customer->setMode(self::MODE_CUSTOMER);
67: break;
68:
69: case self::MODE_REGISTER:
70: case self::MODE_GUEST:
71:
72: $customer = Mage::getModel('customer/customer')
73: ->setData($customerData);
74:
75: if ($customer->getMode() == self::MODE_GUEST) {
76: $password = $customer->generatePassword();
77:
78: $customer
79: ->setPassword($password)
80: ->setConfirmation($password);
81: }
82:
83: $isCustomerValid = $customer->validate();
84: if ($isCustomerValid !== true && is_array($isCustomerValid)) {
85: $this->_fault('customer_data_invalid', implode(PHP_EOL, $isCustomerValid));
86: }
87: break;
88: }
89:
90: try {
91: $quote
92: ->setCustomer($customer)
93: ->setCheckoutMethod($customer->getMode())
94: ->setPasswordHash($customer->encryptPassword($customer->getPassword()))
95: ->save();
96: } catch (Mage_Core_Exception $e) {
97: $this->_fault('customer_not_set', $e->getMessage());
98: }
99:
100: return true;
101: }
102:
103: 104: 105: 106: 107: 108:
109: public function setAddresses($quoteId, $customerAddressData, $store = null)
110: {
111: $quote = $this->_getQuote($quoteId, $store);
112:
113: $customerAddressData = $this->_prepareCustomerAddressData($customerAddressData);
114: if (is_null($customerAddressData)) {
115: $this->_fault('customer_address_data_empty');
116: }
117:
118: foreach ($customerAddressData as $addressItem) {
119:
120:
121:
122: $address = Mage::getModel("sales/quote_address");
123:
124:
125:
126:
127:
128:
129: $addressMode = $addressItem['mode'];
130: unset($addressItem['mode']);
131:
132: if (!empty($addressItem['entity_id'])) {
133: $customerAddress = $this->_getCustomerAddress($addressItem['entity_id']);
134: if ($customerAddress->getCustomerId() != $quote->getCustomerId()) {
135: $this->_fault('address_not_belong_customer');
136: }
137: $address->importCustomerAddress($customerAddress);
138:
139: } else {
140: $address->setData($addressItem);
141: }
142:
143: $address->implodeStreetAddress();
144:
145: if (($validateRes = $address->validate())!==true) {
146: $this->_fault('customer_address_invalid', implode(PHP_EOL, $validateRes));
147: }
148:
149: switch($addressMode) {
150: case self::ADDRESS_BILLING:
151: $address->setEmail($quote->getCustomer()->getEmail());
152:
153: if (!$quote->isVirtual()) {
154: $usingCase = isset($addressItem['use_for_shipping']) ? (int)$addressItem['use_for_shipping'] : 0;
155: switch($usingCase) {
156: case 0:
157: $shippingAddress = $quote->getShippingAddress();
158: $shippingAddress->setSameAsBilling(0);
159: break;
160: case 1:
161: $billingAddress = clone $address;
162: $billingAddress->unsAddressId()->unsAddressType();
163:
164: $shippingAddress = $quote->getShippingAddress();
165: $shippingMethod = $shippingAddress->getShippingMethod();
166: $shippingAddress->addData($billingAddress->getData())
167: ->setSameAsBilling(1)
168: ->setShippingMethod($shippingMethod)
169: ->setCollectShippingRates(true);
170: break;
171: }
172: }
173: $quote->setBillingAddress($address);
174: break;
175:
176: case self::ADDRESS_SHIPPING:
177: $address->setCollectShippingRates(true)
178: ->setSameAsBilling(0);
179: $quote->setShippingAddress($address);
180: break;
181: }
182:
183: }
184:
185: try {
186: $quote
187: ->collectTotals()
188: ->save();
189: } catch (Exception $e) {
190: $this->_fault('address_is_not_set', $e->getMessage());
191: }
192:
193: return true;
194: }
195:
196: 197: 198: 199: 200: 201:
202: protected function _prepareCustomerData($data)
203: {
204: foreach ($this->_attributesMap['quote_customer'] as $attributeAlias=>$attributeCode) {
205: if(isset($data[$attributeAlias]))
206: {
207: $data[$attributeCode] = $data[$attributeAlias];
208: unset($data[$attributeAlias]);
209: }
210: }
211: return $data;
212: }
213:
214: 215: 216: 217: 218: 219:
220: protected function _prepareCustomerAddressData($data)
221: {
222: if (!is_array($data) || !is_array($data[0])) {
223: return null;
224: }
225:
226: $dataAddresses = array();
227: foreach($data as $addressItem) {
228: foreach ($this->_attributesMap['quote_address'] as $attributeAlias=>$attributeCode) {
229: if(isset($addressItem[$attributeAlias]))
230: {
231: $addressItem[$attributeCode] = $addressItem[$attributeAlias];
232: unset($addressItem[$attributeAlias]);
233: }
234: }
235: $dataAddresses[] = $addressItem;
236: }
237: return $dataAddresses;
238: }
239: }
240: