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 class Mage_Checkout_MultishippingController extends Mage_Checkout_Controller_Action
00033 {
00034
00035
00036
00037
00038
00039 protected function _getCheckout()
00040 {
00041 return Mage::getSingleton('checkout/type_multishipping');
00042 }
00043
00044
00045
00046
00047
00048
00049 protected function _getState()
00050 {
00051 return Mage::getSingleton('checkout/type_multishipping_state');
00052 }
00053
00054
00055
00056
00057
00058
00059 protected function _getHelper()
00060 {
00061 return Mage::helper('checkout/url');
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071 public function preDispatch()
00072 {
00073 parent::preDispatch();
00074
00075 $action = $this->getRequest()->getActionName();
00076 if (!preg_match('#^(login|register)#', $action)) {
00077 if (!Mage::getSingleton('customer/session')->authenticate($this, $this->_getHelper()->getMSLoginUrl())) {
00078 $this->setFlag('', self::FLAG_NO_DISPATCH, true);
00079 }
00080
00081 if (!Mage::helper('checkout')->isMultishippingCheckoutAvailable()) {
00082 $this->_redirectUrl($this->_getHelper()->getCartUrl());
00083 $this->setFlag('', self::FLAG_NO_DISPATCH, true);
00084 return $this;
00085 }
00086 }
00087
00088 if (!$this->_preDispatchValidateCustomer()) {
00089 return $this;
00090 }
00091
00092 if (Mage::getSingleton('checkout/session')->getCartWasUpdated(true)
00093 && !in_array($action, array('index', 'login', 'register', 'addresses', 'success'))) {
00094 $this->_redirectUrl($this->_getHelper()->getCartUrl());
00095 $this->setFlag('', self::FLAG_NO_DISPATCH, true);
00096 }
00097
00098 if ($action == 'success' && $this->_getCheckout()->getCheckoutSession()->getDisplaySuccess(true)) {
00099 return $this;
00100 }
00101
00102 $quote = $this->_getCheckout()->getQuote();
00103 if (!$quote->hasItems() || $quote->getHasError() || $quote->isVirtual()) {
00104 $this->_redirectUrl($this->_getHelper()->getCartUrl());
00105 $this->setFlag('', self::FLAG_NO_DISPATCH, true);
00106 return;
00107 }
00108 return $this;
00109 }
00110
00111
00112
00113
00114 public function indexAction()
00115 {
00116 Mage::getSingleton('checkout/session')->setCartWasUpdated(false);
00117 $this->_getCheckout()->getCheckoutSession()->setCheckoutState(
00118 Mage_Checkout_Model_Session::CHECKOUT_STATE_BEGIN
00119 );
00120 $this->_redirect('*/*/addresses');
00121 }
00122
00123
00124
00125
00126 public function loginAction()
00127 {
00128 if (Mage::getSingleton('customer/session')->isLoggedIn()) {
00129 $this->_redirect('*/*/');
00130 return;
00131 }
00132
00133 $this->loadLayout();
00134 $this->_initLayoutMessages('customer/session');
00135
00136
00137 if ($loginForm = $this->getLayout()->getBlock('customer_form_login')) {
00138 $loginForm->setCreateAccountUrl($this->_getHelper()->getMSRegisterUrl());
00139 }
00140 $this->renderLayout();
00141 }
00142
00143
00144
00145
00146 public function registerAction()
00147 {
00148 if (Mage::getSingleton('customer/session')->isLoggedIn()) {
00149 $this->_redirectUrl($this->_getHelper()->getMSCheckoutUrl());
00150 return;
00151 }
00152
00153 $this->loadLayout();
00154 $this->_initLayoutMessages('customer/session');
00155
00156 if ($registerForm = $this->getLayout()->getBlock('customer_form_register')) {
00157 $registerForm->setShowAddressFields(true)
00158 ->setBackUrl($this->_getHelper()->getMSLoginUrl())
00159 ->setSuccessUrl($this->_getHelper()->getMSShippingAddressSavedUrl())
00160 ->setErrorUrl($this->_getHelper()->getCurrentUrl());
00161 }
00162
00163 $this->renderLayout();
00164 }
00165
00166
00167
00168
00169 public function addressesAction()
00170 {
00171
00172 if (!$this->_getCheckout()->getCustomerDefaultShippingAddress()) {
00173 $this->_redirect('*/multishipping_address/newShipping');
00174 return;
00175 }
00176 $this->_getState()->setActiveStep(
00177 Mage_Checkout_Model_Type_Multishipping_State::STEP_SELECT_ADDRESSES
00178 );
00179 if (!$this->_getCheckout()->validateMinimumAmount()) {
00180 $message = $this->_getCheckout()->getMinimumAmountDescription();
00181 $this->_getCheckout()->getCheckoutSession()->addNotice($message);
00182 }
00183 $this->loadLayout();
00184 $this->_initLayoutMessages('customer/session');
00185 $this->_initLayoutMessages('checkout/session');
00186 $this->renderLayout();
00187 }
00188
00189
00190
00191
00192 public function addressesPostAction()
00193 {
00194 try {
00195 if ($this->getRequest()->getParam('continue', false)) {
00196 $this->_getCheckout()->setCollectRatesFlag(true);
00197 $this->_getState()->setActiveStep(
00198 Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
00199 );
00200 $this->_getState()->setCompleteStep(
00201 Mage_Checkout_Model_Type_Multishipping_State::STEP_SELECT_ADDRESSES
00202 );
00203 $this->_redirect('*/*/shipping');
00204 }
00205 elseif ($this->getRequest()->getParam('new_address')) {
00206 $this->_redirect('*/multishipping_address/newShipping');
00207 }
00208 else {
00209 $this->_redirect('*/*/addresses');
00210 }
00211 if ($shipToInfo = $this->getRequest()->getPost('ship')) {
00212 $this->_getCheckout()->setShippingItemsInformation($shipToInfo);
00213 }
00214 }
00215 catch (Mage_Core_Exception $e) {
00216 Mage::getSingleton('checkout/session')->addError($e->getMessage());
00217 $this->_redirect('*/*/addresses');
00218 }
00219 catch (Exception $e) {
00220 Mage::getSingleton('checkout/session')->addException(
00221 $e,
00222 Mage::helper('checkout')->__('Data saving problem')
00223 );
00224 $this->_redirect('*/*/addresses');
00225 }
00226 }
00227
00228 public function backToAddressesAction()
00229 {
00230 $this->_getState()->setActiveStep(
00231 Mage_Checkout_Model_Type_Multishipping_State::STEP_SELECT_ADDRESSES
00232 );
00233 $this->_getState()->unsCompleteStep(
00234 Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
00235 );
00236 $this->_redirect('*/*/addresses');
00237 }
00238
00239
00240
00241
00242 public function removeItemAction()
00243 {
00244 $itemId = $this->getRequest()->getParam('id');
00245 $addressId = $this->getRequest()->getParam('address');
00246 if ($addressId && $itemId) {
00247 $this->_getCheckout()->removeAddressItem($addressId, $itemId);
00248 }
00249 $this->_redirect('*/*/addresses');
00250 }
00251
00252 protected function _validateMinimumAmount()
00253 {
00254 if (!$this->_getCheckout()->validateMinimumAmount()) {
00255 $error = $this->_getCheckout()->getMinimumAmountError();
00256 $this->_getCheckout()->getCheckoutSession()->addError($error);
00257 $this->_forward('backToAddresses');
00258 return false;
00259 }
00260 return true;
00261 }
00262
00263
00264
00265
00266 public function shippingAction()
00267 {
00268 if (!$this->_validateMinimumAmount()) {
00269 return;
00270 }
00271
00272 if (!$this->_getState()->getCompleteStep(Mage_Checkout_Model_Type_Multishipping_State::STEP_SELECT_ADDRESSES)) {
00273 $this->_redirect('*/*/addresses');
00274 return $this;
00275 }
00276
00277 $this->_getState()->setActiveStep(
00278 Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
00279 );
00280 $this->loadLayout();
00281 $this->_initLayoutMessages('customer/session');
00282 $this->_initLayoutMessages('checkout/session');
00283 $this->renderLayout();
00284 }
00285
00286 public function backToShippingAction()
00287 {
00288 $this->_getState()->setActiveStep(
00289 Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
00290 );
00291 $this->_getState()->unsCompleteStep(
00292 Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
00293 );
00294 $this->_redirect('*/*/shipping');
00295 }
00296
00297 public function shippingPostAction()
00298 {
00299 $shippingMethods = $this->getRequest()->getPost('shipping_method');
00300 try {
00301 Mage::dispatchEvent(
00302 'checkout_controller_multishipping_shipping_post',
00303 array('request'=>$this->getRequest(), 'quote'=>$this->_getCheckout()->getQuote())
00304 );
00305 $this->_getCheckout()->setShippingMethods($shippingMethods);
00306 $this->_getState()->setActiveStep(
00307 Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
00308 );
00309 $this->_getState()->setCompleteStep(
00310 Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING
00311 );
00312 $this->_redirect('*/*/billing');
00313 }
00314 catch (Exception $e){
00315 Mage::getSingleton('checkout/session')->addError($e->getMessage());
00316 $this->_redirect('*/*/shipping');
00317 }
00318 }
00319
00320
00321
00322
00323 public function billingAction()
00324 {
00325 if (!$this->_validateBilling()) {
00326 return;
00327 }
00328
00329 if (!$this->_validateMinimumAmount()) {
00330 return;
00331 }
00332
00333 if (!$this->_getState()->getCompleteStep(Mage_Checkout_Model_Type_Multishipping_State::STEP_SHIPPING)) {
00334 $this->_redirect('*/*/shipping');
00335 return $this;
00336 }
00337
00338 $this->_getState()->setActiveStep(
00339 Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
00340 );
00341
00342 $this->loadLayout();
00343 $this->_initLayoutMessages('customer/session');
00344 $this->_initLayoutMessages('checkout/session');
00345 $this->renderLayout();
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 protected function _validateBilling()
00374 {
00375 if(!$this->_getCheckout()->getQuote()->getBillingAddress()->getFirstname()) {
00376 $this->_redirect('*/multishipping_address/selectBilling');
00377 return false;
00378 }
00379 return true;
00380 }
00381
00382 public function backToBillingAction()
00383 {
00384 $this->_getState()->setActiveStep(
00385 Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
00386 );
00387 $this->_getState()->unsCompleteStep(
00388 Mage_Checkout_Model_Type_Multishipping_State::STEP_OVERVIEW
00389 );
00390 $this->_redirect('*/*/billing');
00391 }
00392
00393
00394
00395
00396 public function overviewAction()
00397 {
00398 if (!$this->_validateMinimumAmount()) {
00399 return $this;
00400 }
00401
00402 $this->_getState()->setActiveStep(Mage_Checkout_Model_Type_Multishipping_State::STEP_OVERVIEW);
00403
00404 try {
00405 $payment = $this->getRequest()->getPost('payment');
00406 $this->_getCheckout()->setPaymentMethod($payment);
00407 $this->_getCheckout()->getQuote()->getPayment()->importData($payment);
00408
00409 $this->_getState()->setCompleteStep(
00410 Mage_Checkout_Model_Type_Multishipping_State::STEP_BILLING
00411 );
00412
00413 $this->loadLayout();
00414 $this->_initLayoutMessages('checkout/session');
00415 $this->_initLayoutMessages('customer/session');
00416 $this->renderLayout();
00417 }
00418 catch (Mage_Core_Exception $e) {
00419 Mage::getSingleton('checkout/session')->addError($e->getMessage());
00420 $this->_redirect('*/*/billing');
00421 }
00422 catch (Exception $e) {
00423 Mage::logException($e);
00424 Mage::getSingleton('checkout/session')->addException($e, $this->__('Can\'t open overview page'));
00425 $this->_redirect('*/*/billing');
00426 }
00427 }
00428
00429 public function overviewPostAction()
00430 {
00431 if (!$this->_validateMinimumAmount()) {
00432 return;
00433 }
00434
00435 try {
00436 if ($requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds()) {
00437 $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array()));
00438 if ($diff = array_diff($requiredAgreements, $postedAgreements)) {
00439 Mage::getSingleton('checkout/session')->addError($this->__('Please agree to all Terms and Conditions before placing the order.'));
00440 $this->_redirect('*/*/billing');
00441 return;
00442 }
00443 }
00444
00445 $payment = $this->getRequest()->getPost('payment');
00446 $paymentInstance = $this->_getCheckout()->getQuote()->getPayment();
00447 if (isset($payment['cc_number'])) {
00448 $paymentInstance->setCcNumber($payment['cc_number']);
00449 }
00450 if (isset($payment['cc_cid'])) {
00451 $paymentInstance->setCcCid($payment['cc_cid']);
00452 }
00453 $this->_getCheckout()->createOrders();
00454 $this->_getState()->setActiveStep(
00455 Mage_Checkout_Model_Type_Multishipping_State::STEP_SUCCESS
00456 );
00457 $this->_getState()->setCompleteStep(
00458 Mage_Checkout_Model_Type_Multishipping_State::STEP_OVERVIEW
00459 );
00460 $this->_getCheckout()->getCheckoutSession()->clear();
00461 $this->_getCheckout()->getCheckoutSession()->setDisplaySuccess(true);
00462 $this->_redirect('*/*/success');
00463 }
00464 catch (Mage_Core_Exception $e){
00465 Mage::helper('checkout')->sendPaymentFailedEmail($this->_getCheckout()->getQuote(), $e->getMessage(), 'multi-shipping');
00466 Mage::getSingleton('checkout/session')->addError($e->getMessage());
00467 $this->_redirect('*/*/billing');
00468 }
00469 catch (Exception $e){
00470 Mage::helper('checkout')->sendPaymentFailedEmail($this->_getCheckout()->getQuote(), $e->getMessage(), 'multi-shipping');
00471 Mage::getSingleton('checkout/session')->addError('Order place error.');
00472 $this->_redirect('*/*/billing');
00473 }
00474 }
00475
00476
00477
00478
00479 public function successAction()
00480 {
00481 if (!$this->_getState()->getCompleteStep(Mage_Checkout_Model_Type_Multishipping_State::STEP_OVERVIEW)) {
00482 $this->_redirect('*/*/addresses');
00483 return $this;
00484 }
00485
00486 $this->loadLayout();
00487 $this->_initLayoutMessages('checkout/session');
00488 $this->renderLayout();
00489 }
00490
00491 }