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: class Mage_Checkout_MultishippingController extends Mage_Checkout_Controller_Action
33: {
34: 35: 36: 37: 38:
39: protected function _getCheckout()
40: {
41: return Mage::getSingleton('checkout/type_multishipping');
42: }
43:
44: 45: 46: 47: 48:
49: protected function _getState()
50: {
51: return Mage::getSingleton('checkout/type_multishipping_state');
52: }
53:
54: 55: 56: 57: 58:
59: protected function _getHelper()
60: {
61: return Mage::helper('checkout/url');
62: }
63:
64: 65: 66: 67: 68:
69: protected function _getCheckoutSession()
70: {
71: return Mage::getSingleton('checkout/session');
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public function preDispatch()
82: {
83: parent::preDispatch();
84:
85: if ($this->getFlag('', 'redirectLogin')) {
86: return $this;
87: }
88:
89: $action = $this->getRequest()->getActionName();
90:
91: $checkoutSessionQuote = $this->_getCheckoutSession()->getQuote();
92: 93: 94:
95: if ($action == 'index') {
96: $checkoutSessionQuote->setIsMultiShipping(true);
97: $this->_getCheckoutSession()->setCheckoutState(
98: Mage_Checkout_Model_Session::CHECKOUT_STATE_BEGIN
99: );
100: } elseif (!$checkoutSessionQuote->getIsMultiShipping() &&
101: !in_array($action, array('login', 'register', 'success'))
102: ) {
103: $this->_redirect('*/*/index');
104: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
105: return $this;
106: }
107:
108: if (!in_array($action, array('login', 'register'))) {
109: if (!Mage::getSingleton('customer/session')->authenticate($this, $this->_getHelper()->getMSLoginUrl())) {
110: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
111: }
112:
113: if (!Mage::helper('checkout')->isMultishippingCheckoutAvailable()) {
114: $error = $this->_getCheckout()->getMinimumAmountError();
115: $this->_getCheckoutSession()->addError($error);
116: $this->_redirectUrl($this->_getHelper()->getCartUrl());
117: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
118: return $this;
119: }
120: }
121:
122: if (!$this->_preDispatchValidateCustomer()) {
123: return $this;
124: }
125:
126: if ($this->_getCheckoutSession()->getCartWasUpdated(true) &&
127: !in_array($action, array('index', 'login', 'register', 'addresses', 'success'))
128: ) {
129: $this->_redirectUrl($this->_getHelper()->getCartUrl());
130: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
131: }
132:
133: if ($action == 'success' && $this->_getCheckout()->getCheckoutSession()->getDisplaySuccess(true)) {
134: return $this;
135: }
136:
137: $quote = $this->_getCheckout()->getQuote();
138: if (!$quote->hasItems() || $quote->getHasError() || $quote->isVirtual()) {
139: $this->_redirectUrl($this->_getHelper()->getCartUrl());
140: $this->setFlag('', self::FLAG_NO_DISPATCH, true);
141: return;
142: }
143:
144: return $this;
145: }
146:
147: 148: 149:
150: public function indexAction()
151: {
152: $this->_getCheckoutSession()->setCartWasUpdated(false);
153: $this->_redirect('*/*/addresses');
154: }
155:
156: 157: 158:
159: public function loginAction()
160: {
161: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
162: $this->_redirect('*/*/');
163: return;
164: }
165:
166: $this->loadLayout();
167: $this->_initLayoutMessages('customer/session');
168:
169:
170: if ($loginForm = $this->getLayout()->getBlock('customer_form_login')) {
171: $loginForm->setCreateAccountUrl($this->_getHelper()->getMSRegisterUrl());
172: }
173: $this->renderLayout();
174: }
175:
176: 177: 178:
179: public function registerAction()
180: {
181: if (Mage::getSingleton('customer/session')->isLoggedIn()) {
182: $this->_redirectUrl($this->_getHelper()->getMSCheckoutUrl());
183: return;
184: }
185:
186: $this->loadLayout();
187: $this->_initLayoutMessages('customer/session');
188:
189: if ($registerForm = $this->getLayout()->getBlock('customer_form_register')) {
190: $registerForm->setShowAddressFields(true)
191: ->setBackUrl($this->_getHelper()->getMSLoginUrl())
192: ->setSuccessUrl($this->_getHelper()->getMSShippingAddressSavedUrl())
193: ->setErrorUrl($this->_getHelper()->getCurrentUrl());
194: }
195:
196: $this->renderLayout();
197: }
198:
199: 200: 201:
202: public function addressesAction()
203: {
204:
205: if (!$this->_getCheckout()->getCustomerDefaultShippingAddress()) {
206: $this->_redirect('*/multishipping_address/newShipping');
207: return;
208: }
209:
210: $this->_getState()->unsCompleteStep(
211: Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
212: );
213:
214: $this->_getState()->setActiveStep(
215: Mage_Checkout_Model_Type_Multishipping_State::STEP_SELECT_ADDRESSES
216: );
217: if (!$this->_getCheckout()->validateMinimumAmount()) {
218: $message = $this->_getCheckout()->getMinimumAmountDescription();
219: $this->_getCheckout()->getCheckoutSession()->addNotice($message);
220: }
221: $this->loadLayout();
222: $this->_initLayoutMessages('customer/session');
223: $this->_initLayoutMessages('checkout/session');
224: $this->renderLayout();
225: }
226:
227: 228: 229:
230: public function addressesPostAction()
231: {
232: if (!$this->_getCheckout()->getCustomerDefaultShippingAddress()) {
233: $this->_redirect('*/multishipping_address/newShipping');
234: return;
235: }
236: try {
237: if ($this->getRequest()->getParam('continue', false)) {
238: $this->_getCheckout()->setCollectRatesFlag(true);
239: $this->_getState()->setActiveStep(
240: Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
241: );
242: $this->_getState()->setCompleteStep(
243: Mage_Checkout_Model_Type_Multishipping_State::STEP_SELECT_ADDRESSES
244: );
245: $this->_redirect('*/*/shipping');
246: }
247: elseif ($this->getRequest()->getParam('new_address')) {
248: $this->_redirect('*/multishipping_address/newShipping');
249: }
250: else {
251: $this->_redirect('*/*/addresses');
252: }
253: if ($shipToInfo = $this->getRequest()->getPost('ship')) {
254: $this->_getCheckout()->setShippingItemsInformation($shipToInfo);
255: }
256: }
257: catch (Mage_Core_Exception $e) {
258: $this->_getCheckoutSession()->addError($e->getMessage());
259: $this->_redirect('*/*/addresses');
260: }
261: catch (Exception $e) {
262: $this->_getCheckoutSession()->addException(
263: $e,
264: Mage::helper('checkout')->__('Data saving problem')
265: );
266: $this->_redirect('*/*/addresses');
267: }
268: }
269:
270: public function backToAddressesAction()
271: {
272: $this->_getState()->setActiveStep(
273: Mage_Checkout_Model_Type_Multishipping_State::STEP_SELECT_ADDRESSES
274: );
275: $this->_getState()->unsCompleteStep(
276: Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
277: );
278: $this->_redirect('*/*/addresses');
279: }
280:
281: 282: 283:
284: public function removeItemAction()
285: {
286: $itemId = $this->getRequest()->getParam('id');
287: $addressId = $this->getRequest()->getParam('address');
288: if ($addressId && $itemId) {
289: $this->_getCheckout()->setCollectRatesFlag(true);
290: $this->_getCheckout()->removeAddressItem($addressId, $itemId);
291: }
292: $this->_redirect('*/*/addresses');
293: }
294:
295: protected function _validateMinimumAmount()
296: {
297: if (!$this->_getCheckout()->validateMinimumAmount()) {
298: $error = $this->_getCheckout()->getMinimumAmountError();
299: $this->_getCheckout()->getCheckoutSession()->addError($error);
300: $this->_forward('backToAddresses');
301: return false;
302: }
303: return true;
304: }
305:
306: 307: 308:
309: public function shippingAction()
310: {
311: if (!$this->_validateMinimumAmount()) {
312: return;
313: }
314:
315: if (!$this->_getState()->getCompleteStep(Mage_Checkout_Model_Type_Multishipping_State::STEP_SELECT_ADDRESSES)) {
316: $this->_redirect('*/*/addresses');
317: return $this;
318: }
319:
320: $this->_getState()->setActiveStep(
321: Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
322: );
323: $this->loadLayout();
324: $this->_initLayoutMessages('customer/session');
325: $this->_initLayoutMessages('checkout/session');
326: $this->renderLayout();
327: }
328:
329: public function backToShippingAction()
330: {
331: $this->_getState()->setActiveStep(
332: Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
333: );
334: $this->_getState()->unsCompleteStep(
335: Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
336: );
337: $this->_redirect('*/*/shipping');
338: }
339:
340: public function shippingPostAction()
341: {
342: $shippingMethods = $this->getRequest()->getPost('shipping_method');
343: try {
344: Mage::dispatchEvent(
345: 'checkout_controller_multishipping_shipping_post',
346: array('request'=>$this->getRequest(), 'quote'=>$this->_getCheckout()->getQuote())
347: );
348: $this->_getCheckout()->setShippingMethods($shippingMethods);
349: $this->_getState()->setActiveStep(
350: Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
351: );
352: $this->_getState()->setCompleteStep(
353: Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
354: );
355: $this->_redirect('*/*/billing');
356: }
357: catch (Exception $e){
358: $this->_getCheckoutSession()->addError($e->getMessage());
359: $this->_redirect('*/*/shipping');
360: }
361: }
362:
363: 364: 365:
366: public function billingAction()
367: {
368: if (!$this->_validateBilling()) {
369: return;
370: }
371:
372: if (!$this->_validateMinimumAmount()) {
373: return;
374: }
375:
376: if (!$this->_getState()->getCompleteStep(Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING)) {
377: $this->_redirect('*/*/shipping');
378: return $this;
379: }
380:
381: $this->_getState()->setActiveStep(
382: Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
383: );
384:
385: $this->loadLayout();
386: $this->_initLayoutMessages('customer/session');
387: $this->_initLayoutMessages('checkout/session');
388: $this->renderLayout();
389: }
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411: 412: 413: 414: 415:
416: protected function _validateBilling()
417: {
418: if(!$this->_getCheckout()->getQuote()->getBillingAddress()->getFirstname()) {
419: $this->_redirect('*/multishipping_address/selectBilling');
420: return false;
421: }
422: return true;
423: }
424:
425: public function backToBillingAction()
426: {
427: $this->_getState()->setActiveStep(
428: Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
429: );
430: $this->_getState()->unsCompleteStep(
431: Mage_Checkout_Model_Type_Multishipping_State::STEP_OVERVIEW
432: );
433: $this->_redirect('*/*/billing');
434: }
435:
436: 437: 438:
439: public function overviewAction()
440: {
441: if (!$this->_validateMinimumAmount()) {
442: return $this;
443: }
444:
445: $this->_getState()->setActiveStep(Mage_Checkout_Model_Type_Multishipping_State::STEP_OVERVIEW);
446:
447: try {
448: $payment = $this->getRequest()->getPost('payment');
449: $this->_getCheckout()->setPaymentMethod($payment);
450:
451: $this->_getState()->setCompleteStep(
452: Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
453: );
454:
455: $this->loadLayout();
456: $this->_initLayoutMessages('checkout/session');
457: $this->_initLayoutMessages('customer/session');
458: $this->renderLayout();
459: }
460: catch (Mage_Core_Exception $e) {
461: $this->_getCheckoutSession()->addError($e->getMessage());
462: $this->_redirect('*/*/billing');
463: }
464: catch (Exception $e) {
465: Mage::logException($e);
466: $this->_getCheckoutSession()->addException($e, $this->__('Cannot open the overview page'));
467: $this->_redirect('*/*/billing');
468: }
469: }
470:
471: public function overviewPostAction()
472: {
473: if (!$this->_validateMinimumAmount()) {
474: return;
475: }
476:
477: try {
478: if ($requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds()) {
479: $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array()));
480: if ($diff = array_diff($requiredAgreements, $postedAgreements)) {
481: $this->_getCheckoutSession()->addError($this->__('Please agree to all Terms and Conditions before placing the order.'));
482: $this->_redirect('*/*/billing');
483: return;
484: }
485: }
486:
487: $payment = $this->getRequest()->getPost('payment');
488: $paymentInstance = $this->_getCheckout()->getQuote()->getPayment();
489: if (isset($payment['cc_number'])) {
490: $paymentInstance->setCcNumber($payment['cc_number']);
491: }
492: if (isset($payment['cc_cid'])) {
493: $paymentInstance->setCcCid($payment['cc_cid']);
494: }
495: $this->_getCheckout()->createOrders();
496: $this->_getState()->setActiveStep(
497: Mage_Checkout_Model_Type_Multishipping_State::STEP_SUCCESS
498: );
499: $this->_getState()->setCompleteStep(
500: Mage_Checkout_Model_Type_Multishipping_State::STEP_OVERVIEW
501: );
502: $this->_getCheckout()->getCheckoutSession()->clear();
503: $this->_getCheckout()->getCheckoutSession()->setDisplaySuccess(true);
504: $this->_redirect('*/*/success');
505: } catch (Mage_Payment_Model_Info_Exception $e) {
506: $message = $e->getMessage();
507: if( !empty($message) ) {
508: $this->_getCheckoutSession()->addError($message);
509: }
510: $this->_redirect('*/*/billing');
511: } catch (Mage_Checkout_Exception $e) {
512: Mage::helper('checkout')
513: ->sendPaymentFailedEmail($this->_getCheckout()->getQuote(), $e->getMessage(), 'multi-shipping');
514: $this->_getCheckout()->getCheckoutSession()->clear();
515: $this->_getCheckoutSession()->addError($e->getMessage());
516: $this->_redirect('*/cart');
517: }
518: catch (Mage_Core_Exception $e){
519: Mage::helper('checkout')
520: ->sendPaymentFailedEmail($this->_getCheckout()->getQuote(), $e->getMessage(), 'multi-shipping');
521: $this->_getCheckoutSession()->addError($e->getMessage());
522: $this->_redirect('*/*/billing');
523: } catch (Exception $e){
524: Mage::logException($e);
525: Mage::helper('checkout')
526: ->sendPaymentFailedEmail($this->_getCheckout()->getQuote(), $e->getMessage(), 'multi-shipping');
527: $this->_getCheckoutSession()->addError($this->__('Order place error.'));
528: $this->_redirect('*/*/billing');
529: }
530: }
531:
532: 533: 534:
535: public function successAction()
536: {
537: if (!$this->_getState()->getCompleteStep(Mage_Checkout_Model_Type_Multishipping_State::STEP_OVERVIEW)) {
538: $this->_redirect('*/*/addresses');
539: return $this;
540: }
541:
542: $this->loadLayout();
543: $this->_initLayoutMessages('checkout/session');
544: $ids = $this->_getCheckout()->getOrderIds();
545: Mage::dispatchEvent('checkout_multishipping_controller_success_action', array('order_ids' => $ids));
546: $this->renderLayout();
547: }
548:
549: 550: 551: 552:
553: public function redirectLogin()
554: {
555: $this->setFlag('', 'no-dispatch', true);
556: Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::getUrl('*/*', array('_secure'=>true)));
557:
558: $this->getResponse()->setRedirect(
559: Mage::helper('core/url')->addRequestParam(
560: $this->_getHelper()->getMSLoginUrl(),
561: array('context' => 'checkout')
562: )
563: );
564:
565: $this->setFlag('', 'redirectLogin', true);
566: }
567: }
568: