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_Api_Resource_Customer extends Mage_Checkout_Model_Api_Resource
35: {
36: 37: 38:
39: const ADDRESS_BILLING = Mage_Sales_Model_Quote_Address::TYPE_BILLING;
40: const ADDRESS_SHIPPING = Mage_Sales_Model_Quote_Address::TYPE_SHIPPING;
41:
42: 43: 44:
45: const MODE_CUSTOMER = Mage_Checkout_Model_Type_Onepage::METHOD_CUSTOMER;
46: const MODE_REGISTER = Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER;
47: const MODE_GUEST = Mage_Checkout_Model_Type_Onepage::METHOD_GUEST;
48:
49:
50: 51: 52:
53: protected function _getCustomer($customerId)
54: {
55:
56: $customer = Mage::getModel('customer/customer')
57: ->load($customerId);
58: if (!$customer->getId()) {
59: $this->_fault('customer_not_exists');
60: }
61:
62: return $customer;
63: }
64:
65: 66: 67: 68: 69: 70:
71: protected function _getCustomerAddress($addressId)
72: {
73: $address = Mage::getModel('customer/address')->load((int)$addressId);
74: if (is_null($address->getId())) {
75: $this->_fault('invalid_address_id');
76: }
77:
78: $address->explodeStreetAddress();
79: if ($address->getRegionId()) {
80: $address->setRegion($address->getRegionId());
81: }
82: return $address;
83: }
84:
85: 86: 87: 88:
89: public function prepareCustomerForQuote(Mage_Sales_Model_Quote $quote)
90: {
91: $isNewCustomer = false;
92: switch ($quote->getCheckoutMethod()) {
93: case self::MODE_GUEST:
94: $this->_prepareGuestQuote($quote);
95: break;
96: case self::MODE_REGISTER:
97: $this->_prepareNewCustomerQuote($quote);
98: $isNewCustomer = true;
99: break;
100: default:
101: $this->_prepareCustomerQuote($quote);
102: break;
103: }
104:
105: return $isNewCustomer;
106: }
107:
108: 109: 110: 111: 112: 113:
114: protected function _prepareGuestQuote(Mage_Sales_Model_Quote $quote)
115: {
116: $quote->setCustomerId(null)
117: ->setCustomerEmail($quote->getBillingAddress()->getEmail())
118: ->setCustomerIsGuest(true)
119: ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
120: return $this;
121: }
122:
123: 124: 125: 126: 127: 128:
129: protected function _prepareNewCustomerQuote(Mage_Sales_Model_Quote $quote)
130: {
131: $billing = $quote->getBillingAddress();
132: $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress();
133:
134:
135: $customer = $quote->getCustomer();
136:
137: $customerBilling = $billing->exportCustomerAddress();
138: $customer->addAddress($customerBilling);
139: $billing->setCustomerAddress($customerBilling);
140: $customerBilling->setIsDefaultBilling(true);
141: if ($shipping && !$shipping->getSameAsBilling()) {
142: $customerShipping = $shipping->exportCustomerAddress();
143: $customer->addAddress($customerShipping);
144: $shipping->setCustomerAddress($customerShipping);
145: $customerShipping->setIsDefaultShipping(true);
146: } else {
147: $customerBilling->setIsDefaultShipping(true);
148: }
149:
150: Mage::helper('core')->copyFieldset('checkout_onepage_quote', 'to_customer', $quote, $customer);
151: $customer->setPassword($customer->decryptPassword($quote->getPasswordHash()));
152: $customer->setPasswordHash($customer->hashPassword($customer->getPassword()));
153: $quote->setCustomer($customer)
154: ->setCustomerId(true);
155:
156: return $this;
157: }
158:
159: 160: 161: 162: 163: 164:
165: protected function _prepareCustomerQuote(Mage_Sales_Model_Quote $quote)
166: {
167: $billing = $quote->getBillingAddress();
168: $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress();
169:
170: $customer = $quote->getCustomer();
171: if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) {
172: $customerBilling = $billing->exportCustomerAddress();
173: $customer->addAddress($customerBilling);
174: $billing->setCustomerAddress($customerBilling);
175: }
176: if ($shipping && ((!$shipping->getCustomerId() && !$shipping->getSameAsBilling())
177: || (!$shipping->getSameAsBilling() && $shipping->getSaveInAddressBook()))) {
178: $customerShipping = $shipping->exportCustomerAddress();
179: $customer->addAddress($customerShipping);
180: $shipping->setCustomerAddress($customerShipping);
181: }
182:
183: if (isset($customerBilling) && !$customer->getDefaultBilling()) {
184: $customerBilling->setIsDefaultBilling(true);
185: }
186: if ($shipping && isset($customerShipping) && !$customer->getDefaultShipping()) {
187: $customerShipping->setIsDefaultShipping(true);
188: } else if (isset($customerBilling) && !$customer->getDefaultShipping()) {
189: $customerBilling->setIsDefaultShipping(true);
190: }
191: $quote->setCustomer($customer);
192:
193: return $this;
194: }
195:
196: 197: 198: 199: 200: 201:
202: public function involveNewCustomer(Mage_Sales_Model_Quote $quote)
203: {
204: $customer = $quote->getCustomer();
205: if ($customer->isConfirmationRequired()) {
206: $customer->sendNewAccountEmail('confirmation');
207: } else {
208: $customer->sendNewAccountEmail();
209: }
210:
211: return $this;
212: }
213: }
214: