Public Member Functions | |
getCheckout () | |
getQuote () | |
initCheckout () | |
saveCheckoutMethod ($method) | |
getAddress ($addressId) | |
saveBilling ($data, $customerAddressId) | |
saveShipping ($data, $customerAddressId) | |
saveShippingMethod ($shippingMethod) | |
savePayment ($data) | |
saveOrder () | |
getLastOrderId () | |
Protected Member Functions | |
_processValidateCustomer (Mage_Sales_Model_Quote_Address $address) | |
validateOrder () | |
_customerEmailExists ($email, $websiteId=null) |
Definition at line 28 of file Onepage.php.
_customerEmailExists | ( | $ | email, | |
$ | websiteId = null | |||
) | [protected] |
Check if customer email exists
string | ||
int | $websiteId |
Definition at line 616 of file Onepage.php.
00617 { 00618 $customer = Mage::getModel('customer/customer'); 00619 if ($websiteId) { 00620 $customer->setWebsiteId($websiteId); 00621 } 00622 $customer->loadByEmail($email); 00623 if ($customer->getId()) { 00624 return $customer; 00625 } 00626 return false; 00627 }
_processValidateCustomer | ( | Mage_Sales_Model_Quote_Address $ | address | ) | [protected] |
Validate customer data and set some its data for further usage in quote Will return either true or array with error messages
Mage_Sales_Model_Quote_Address | $address |
Definition at line 217 of file Onepage.php.
00218 { 00219 // set customer date of birth for further usage 00220 $dob = ''; 00221 if ($address->getDob()) { 00222 $dob = Mage::app()->getLocale()->date($address->getDob(), null, null, false)->toString('yyyy-MM-dd'); 00223 $this->getQuote()->setCustomerDob($dob); 00224 } 00225 00226 // set customer tax/vat number for further usage 00227 if ($address->getTaxvat()) { 00228 $this->getQuote()->setCustomerTaxvat($address->getTaxvat()); 00229 } 00230 00231 // invoke customer model, if it is registering 00232 if (Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER == $this->getQuote()->getCheckoutMethod()) { 00233 // set customer password hash for further usage 00234 $customer = Mage::getModel('customer/customer'); 00235 $this->getQuote()->setPasswordHash($customer->encryptPassword($address->getCustomerPassword())); 00236 00237 // validate customer 00238 foreach (array( 00239 'firstname' => 'firstname', 00240 'lastname' => 'lastname', 00241 'email' => 'email', 00242 'password' => 'customer_password', 00243 'confirmation' => 'confirm_password', 00244 'taxvat' => 'taxvat', 00245 ) as $key => $dataKey) { 00246 $customer->setData($key, $address->getData($dataKey)); 00247 } 00248 if ($dob) { 00249 $customer->setDob($dob); 00250 } 00251 $validationResult = $customer->validate(); 00252 if (true !== $validationResult && is_array($validationResult)) { 00253 return array( 00254 'error' => -1, 00255 'message' => implode(', ', $validationResult) 00256 ); 00257 } 00258 } elseif(Mage_Sales_Model_Quote::CHECKOUT_METHOD_GUEST == $this->getQuote()->getCheckoutMethod()) { 00259 $email = $address->getData('email'); 00260 if (!Zend_Validate::is($email, 'EmailAddress')) { 00261 return array( 00262 'error' => -1, 00263 'message' => Mage::helper('checkout')->__('Invalid email address "%s"', $email) 00264 ); 00265 } 00266 } 00267 00268 return true; 00269 }
getAddress | ( | $ | addressId | ) |
Enter description here...
int | $addressId |
Definition at line 108 of file Onepage.php.
00109 { 00110 $address = Mage::getModel('customer/address')->load((int)$addressId); 00111 $address->explodeStreetAddress(); 00112 if ($address->getRegionId()) { 00113 $address->setRegion($address->getRegionId()); 00114 } 00115 return $address; 00116 }
getCheckout | ( | ) |
Enter description here...
Definition at line 35 of file Onepage.php.
00036 { 00037 return Mage::getSingleton('checkout/session'); 00038 }
getLastOrderId | ( | ) |
Enter description here...
Definition at line 634 of file Onepage.php.
00635 { 00636 /* 00637 $customerSession = Mage::getSingleton('customer/session'); 00638 if (!$customerSession->isLoggedIn()) { 00639 $this->_redirect('checkout/cart'); 00640 return; 00641 } 00642 $collection = Mage::getResourceModel('sales/order_collection') 00643 ->addAttributeSelect('self/real_order_id') 00644 ->addAttributeFilter('self/customer_id', $customerSession->getCustomerId()) 00645 ->setOrder('self/created_at', 'DESC') 00646 ->setPageSize(1) 00647 ->loadData(); 00648 foreach ($collection as $order) { 00649 $orderId = $order->getRealOrderId(); 00650 } 00651 */ 00652 $order = Mage::getModel('sales/order'); 00653 $order->load($this->getCheckout()->getLastOrderId()); 00654 $orderId = $order->getIncrementId(); 00655 return $orderId; 00656 }
getQuote | ( | ) |
Enter description here...
Definition at line 45 of file Onepage.php.
00046 { 00047 return $this->getCheckout()->getQuote(); 00048 }
initCheckout | ( | ) |
Enter description here...
Definition at line 55 of file Onepage.php.
00056 { 00057 $checkout = $this->getCheckout(); 00058 if (is_array($checkout->getStepData())) { 00059 foreach ($checkout->getStepData() as $step=>$data) { 00060 if (!($step==='login' 00061 || Mage::getSingleton('customer/session')->isLoggedIn() && $step==='billing')) { 00062 $checkout->setStepData($step, 'allow', false); 00063 } 00064 } 00065 } 00066 /* 00067 * want to laod the correct customer information by assiging to address 00068 * instead of just loading from sales/quote_address 00069 */ 00070 $customer = Mage::getSingleton('customer/session')->getCustomer(); 00071 if ($customer) { 00072 $this->getQuote()->assignCustomer($customer); 00073 } 00074 if ($this->getQuote()->getIsMultiShipping()) { 00075 $this->getQuote()->setIsMultiShipping(false); 00076 $this->getQuote()->save(); 00077 } 00078 return $this; 00079 }
saveBilling | ( | $ | data, | |
$ | customerAddressId | |||
) |
This method is called by One Page Checkout JS (AJAX) while saving the billing information.
unknown_type | $data | |
unknown_type | $customerAddressId |
Billing address using otions
Definition at line 125 of file Onepage.php.
00126 { 00127 if (empty($data)) { 00128 $res = array( 00129 'error' => -1, 00130 'message' => Mage::helper('checkout')->__('Invalid data') 00131 ); 00132 return $res; 00133 } 00134 00135 $address = $this->getQuote()->getBillingAddress(); 00136 if (!empty($customerAddressId)) { 00137 $customerAddress = Mage::getModel('customer/address')->load($customerAddressId); 00138 if ($customerAddress->getId()) { 00139 if ($customerAddress->getCustomerId() != $this->getQuote()->getCustomerId()) { 00140 return array('error' => 1, 00141 'message' => Mage::helper('checkout')->__('Customer Address is not valid.') 00142 ); 00143 } 00144 $address->importCustomerAddress($customerAddress); 00145 } 00146 } else { 00147 unset($data['address_id']); 00148 $address->addData($data); 00149 //$address->setId(null); 00150 } 00151 00152 if (($validateRes = $address->validate())!==true) { 00153 $res = array( 00154 'error' => 1, 00155 'message' => $validateRes 00156 ); 00157 return $res; 00158 } 00159 00160 if (!$this->getQuote()->getCustomerId() && Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER == $this->getQuote()->getCheckoutMethod()) { 00161 if ($this->_customerEmailExists($address->getEmail(), Mage::app()->getWebsite()->getId())) { 00162 return array('error' => 1, 00163 'message' => Mage::helper('checkout')->__('There is already a customer registered using this email address') 00164 ); 00165 } 00166 } 00167 00168 $address->implodeStreetAddress(); 00169 00170 if (!$this->getQuote()->isVirtual()) { 00171 /** 00172 * Billing address using otions 00173 */ 00174 $usingCase = isset($data['use_for_shipping']) ? (int) $data['use_for_shipping'] : 0; 00175 00176 switch($usingCase) { 00177 case 0: 00178 $shipping = $this->getQuote()->getShippingAddress(); 00179 $shipping->setSameAsBilling(0); 00180 break; 00181 case 1: 00182 $billing = clone $address; 00183 $billing->unsAddressId()->unsAddressType(); 00184 $shipping = $this->getQuote()->getShippingAddress(); 00185 $shippingMethod = $shipping->getShippingMethod(); 00186 $shipping->addData($billing->getData()) 00187 ->setSameAsBilling(1) 00188 ->setShippingMethod($shippingMethod) 00189 ->setCollectShippingRates(true); 00190 $this->getCheckout()->setStepData('shipping', 'complete', true); 00191 break; 00192 } 00193 } 00194 00195 if (true !== $result = $this->_processValidateCustomer($address)) { 00196 return $result; 00197 } 00198 00199 $this->getQuote()->collectTotals(); 00200 $this->getQuote()->save(); 00201 00202 $this->getCheckout() 00203 ->setStepData('billing', 'allow', true) 00204 ->setStepData('billing', 'complete', true) 00205 ->setStepData('shipping', 'allow', true); 00206 00207 return array(); 00208 }
saveCheckoutMethod | ( | $ | method | ) |
Enter description here...
string | $method |
Definition at line 87 of file Onepage.php.
00088 { 00089 if (empty($method)) { 00090 $res = array( 00091 'error' => -1, 00092 'message' => Mage::helper('checkout')->__('Invalid data') 00093 ); 00094 return $res; 00095 } 00096 00097 $this->getQuote()->setCheckoutMethod($method)->save(); 00098 $this->getCheckout()->setStepData('billing', 'allow', true); 00099 return array(); 00100 }
saveOrder | ( | ) |
Enter description here...
We can use configuration data for declare new order status
a flag to set that there will be redirect to third party after confirmation eg: paypal standard ipn
need to have somelogic to set order as new status to make sure order is not finished yet quote will be still active when we send the customer to paypal
we only want to send to customer about new order when there is no redirect to third party
we need to save quote here to have it saved with Customer Id. so when loginById() executes checkout/session method loadCustomerQuote it would not create new quotes and merge it with old one.
Definition at line 400 of file Onepage.php.
00401 { 00402 00403 $this->validateOrder(); 00404 $billing = $this->getQuote()->getBillingAddress(); 00405 if (!$this->getQuote()->isVirtual()) { 00406 $shipping = $this->getQuote()->getShippingAddress(); 00407 } 00408 switch ($this->getQuote()->getCheckoutMethod()) { 00409 case Mage_Sales_Model_Quote::CHECKOUT_METHOD_GUEST: 00410 if (!$this->getQuote()->isAllowedGuestCheckout()) { 00411 Mage::throwException(Mage::helper('checkout')->__('Sorry, guest checkout is not enabled. Please try again or contact store owner.')); 00412 } 00413 $this->getQuote()->setCustomerId(null) 00414 ->setCustomerEmail($billing->getEmail()) 00415 ->setCustomerIsGuest(true) 00416 ->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID); 00417 break; 00418 00419 case Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER: 00420 $customer = Mage::getModel('customer/customer'); 00421 /* @var $customer Mage_Customer_Model_Customer */ 00422 00423 $customerBilling = $billing->exportCustomerAddress(); 00424 $customer->addAddress($customerBilling); 00425 00426 if (!$this->getQuote()->isVirtual() && !$shipping->getSameAsBilling()) { 00427 $customerShipping = $shipping->exportCustomerAddress(); 00428 $customer->addAddress($customerShipping); 00429 } 00430 00431 if ($this->getQuote()->getCustomerDob() && !$billing->getCustomerDob()) { 00432 $billing->setCustomerDob($this->getQuote()->getCustomerDob()); 00433 } 00434 00435 if ($this->getQuote()->getCustomerTaxvat() && !$billing->getCustomerTaxvat()) { 00436 $billing->setCustomerTaxvat($this->getQuote()->getCustomerTaxvat()); 00437 } 00438 00439 Mage::helper('core')->copyFieldset('checkout_onepage_billing', 'to_customer', $billing, $customer); 00440 00441 $customer->setPassword($customer->decryptPassword($this->getQuote()->getPasswordHash())); 00442 $customer->setPasswordHash($customer->hashPassword($customer->getPassword())); 00443 00444 $this->getQuote()->setCustomer($customer); 00445 break; 00446 00447 default: 00448 $customer = Mage::getSingleton('customer/session')->getCustomer(); 00449 00450 if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) { 00451 $customerBilling = $billing->exportCustomerAddress(); 00452 $customer->addAddress($customerBilling); 00453 } 00454 if (!$this->getQuote()->isVirtual() && 00455 ((!$shipping->getCustomerId() && !$shipping->getSameAsBilling()) || 00456 (!$shipping->getSameAsBilling() && $shipping->getSaveInAddressBook()))) { 00457 00458 $customerShipping = $shipping->exportCustomerAddress(); 00459 $customer->addAddress($customerShipping); 00460 } 00461 $customer->setSavedFromQuote(true); 00462 $customer->save(); 00463 00464 $changed = false; 00465 if (isset($customerBilling) && !$customer->getDefaultBilling()) { 00466 $customer->setDefaultBilling($customerBilling->getId()); 00467 $changed = true; 00468 } 00469 if (!$this->getQuote()->isVirtual() && isset($customerBilling) && !$customer->getDefaultShipping() && $shipping->getSameAsBilling()) { 00470 $customer->setDefaultShipping($customerBilling->getId()); 00471 $changed = true; 00472 } 00473 elseif (!$this->getQuote()->isVirtual() && isset($customerShipping) && !$customer->getDefaultShipping()){ 00474 $customer->setDefaultShipping($customerShipping->getId()); 00475 $changed = true; 00476 } 00477 00478 if ($changed) { 00479 $customer->save(); 00480 } 00481 } 00482 00483 $this->getQuote()->reserveOrderId(); 00484 $convertQuote = Mage::getModel('sales/convert_quote'); 00485 /* @var $convertQuote Mage_Sales_Model_Convert_Quote */ 00486 //$order = Mage::getModel('sales/order'); 00487 if ($this->getQuote()->isVirtual()) { 00488 $order = $convertQuote->addressToOrder($billing); 00489 } 00490 else { 00491 $order = $convertQuote->addressToOrder($shipping); 00492 } 00493 /* @var $order Mage_Sales_Model_Order */ 00494 $order->setBillingAddress($convertQuote->addressToOrderAddress($billing)); 00495 00496 if (!$this->getQuote()->isVirtual()) { 00497 $order->setShippingAddress($convertQuote->addressToOrderAddress($shipping)); 00498 } 00499 00500 $order->setPayment($convertQuote->paymentToOrderPayment($this->getQuote()->getPayment())); 00501 00502 foreach ($this->getQuote()->getAllItems() as $item) { 00503 $orderItem = $convertQuote->itemToOrderItem($item); 00504 if ($item->getParentItem()) { 00505 $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId())); 00506 } 00507 $order->addItem($orderItem); 00508 } 00509 00510 /** 00511 * We can use configuration data for declare new order status 00512 */ 00513 Mage::dispatchEvent('checkout_type_onepage_save_order', array('order'=>$order, 'quote'=>$this->getQuote())); 00514 // check again, if customer exists 00515 if ($this->getQuote()->getCheckoutMethod() == Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER) { 00516 if ($this->_customerEmailExists($customer->getEmail(), Mage::app()->getWebsite()->getId())) { 00517 Mage::throwException(Mage::helper('checkout')->__('There is already a customer registered using this email address')); 00518 } 00519 } 00520 $order->place(); 00521 00522 if ($this->getQuote()->getCheckoutMethod()==Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER) { 00523 $customer->save(); 00524 $customerBillingId = $customerBilling->getId(); 00525 if (!$this->getQuote()->isVirtual()) { 00526 $customerShippingId = isset($customerShipping) ? $customerShipping->getId() : $customerBillingId; 00527 $customer->setDefaultShipping($customerShippingId); 00528 } 00529 $customer->setDefaultBilling($customerBillingId); 00530 $customer->save(); 00531 00532 $this->getQuote()->setCustomerId($customer->getId()); 00533 00534 $order->setCustomerId($customer->getId()); 00535 Mage::helper('core')->copyFieldset('customer_account', 'to_order', $customer, $order); 00536 00537 $billing->setCustomerId($customer->getId())->setCustomerAddressId($customerBillingId); 00538 if (!$this->getQuote()->isVirtual()) { 00539 $shipping->setCustomerId($customer->getId())->setCustomerAddressId($customerShippingId); 00540 } 00541 00542 if ($customer->isConfirmationRequired()) { 00543 $customer->sendNewAccountEmail('confirmation'); 00544 } 00545 else { 00546 $customer->sendNewAccountEmail(); 00547 } 00548 } 00549 00550 /** 00551 * a flag to set that there will be redirect to third party after confirmation 00552 * eg: paypal standard ipn 00553 */ 00554 $redirectUrl = $this->getQuote()->getPayment()->getOrderPlaceRedirectUrl(); 00555 if(!$redirectUrl){ 00556 $order->setEmailSent(true); 00557 } 00558 00559 $order->save(); 00560 00561 Mage::dispatchEvent('checkout_type_onepage_save_order_after', array('order'=>$order, 'quote'=>$this->getQuote())); 00562 00563 /** 00564 * need to have somelogic to set order as new status to make sure order is not finished yet 00565 * quote will be still active when we send the customer to paypal 00566 */ 00567 00568 $orderId = $order->getIncrementId(); 00569 $this->getCheckout()->setLastQuoteId($this->getQuote()->getId()); 00570 $this->getCheckout()->setLastOrderId($order->getId()); 00571 $this->getCheckout()->setLastRealOrderId($order->getIncrementId()); 00572 $this->getCheckout()->setRedirectUrl($redirectUrl); 00573 00574 /** 00575 * we only want to send to customer about new order when there is no redirect to third party 00576 */ 00577 if(!$redirectUrl){ 00578 $order->sendNewOrderEmail(); 00579 } 00580 00581 if ($this->getQuote()->getCheckoutMethod(true)==Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER 00582 && !Mage::getSingleton('customer/session')->isLoggedIn()) { 00583 /** 00584 * we need to save quote here to have it saved with Customer Id. 00585 * so when loginById() executes checkout/session method loadCustomerQuote 00586 * it would not create new quotes and merge it with old one. 00587 */ 00588 $this->getQuote()->save(); 00589 if ($customer->isConfirmationRequired()) { 00590 Mage::getSingleton('checkout/session')->addSuccess(Mage::helper('customer')->__('Account confirmation is required. Please, check your e-mail for confirmation link. To resend confirmation email please <a href="%s">click here</a>.', 00591 Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail()) 00592 )); 00593 } 00594 else { 00595 Mage::getSingleton('customer/session')->loginById($customer->getId()); 00596 } 00597 } 00598 00599 //Setting this one more time like control flag that we haves saved order 00600 //Must be checkout on success page to show it or not. 00601 $this->getCheckout()->setLastSuccessQuoteId($this->getQuote()->getId()); 00602 00603 $this->getQuote()->setIsActive(false); 00604 $this->getQuote()->save(); 00605 00606 return $this; 00607 }
savePayment | ( | $ | data | ) |
Definition at line 343 of file Onepage.php.
00344 { 00345 if (empty($data)) { 00346 $res = array( 00347 'error' => -1, 00348 'message' => Mage::helper('checkout')->__('Invalid data') 00349 ); 00350 return $res; 00351 } 00352 $payment = $this->getQuote()->getPayment(); 00353 $payment->importData($data); 00354 00355 $this->getQuote()->getShippingAddress()->setPaymentMethod($payment->getMethod()); 00356 $this->getQuote()->collectTotals()->save(); 00357 00358 $this->getCheckout() 00359 ->setStepData('payment', 'complete', true) 00360 ->setStepData('review', 'allow', true); 00361 00362 return array(); 00363 }
saveShipping | ( | $ | data, | |
$ | customerAddressId | |||
) |
Definition at line 271 of file Onepage.php.
00272 { 00273 if (empty($data)) { 00274 $res = array( 00275 'error' => -1, 00276 'message' => Mage::helper('checkout')->__('Invalid data') 00277 ); 00278 return $res; 00279 } 00280 $address = $this->getQuote()->getShippingAddress(); 00281 00282 if (!empty($customerAddressId)) { 00283 $customerAddress = Mage::getModel('customer/address')->load($customerAddressId); 00284 if ($customerAddress->getId()) { 00285 if ($customerAddress->getCustomerId() != $this->getQuote()->getCustomerId()) { 00286 return array('error' => 1, 00287 'message' => Mage::helper('checkout')->__('Customer Address is not valid.') 00288 ); 00289 } 00290 $address->importCustomerAddress($customerAddress); 00291 } 00292 } else { 00293 unset($data['address_id']); 00294 $address->addData($data); 00295 } 00296 $address->implodeStreetAddress(); 00297 $address->setCollectShippingRates(true); 00298 00299 if (($validateRes = $address->validate())!==true) { 00300 $res = array( 00301 'error' => 1, 00302 'message' => $validateRes 00303 ); 00304 return $res; 00305 } 00306 00307 $this->getQuote()->collectTotals()->save(); 00308 00309 $this->getCheckout() 00310 ->setStepData('shipping', 'complete', true) 00311 ->setStepData('shipping_method', 'allow', true); 00312 00313 return array(); 00314 }
saveShippingMethod | ( | $ | shippingMethod | ) |
Definition at line 316 of file Onepage.php.
00317 { 00318 if (empty($shippingMethod)) { 00319 $res = array( 00320 'error' => -1, 00321 'message' => Mage::helper('checkout')->__('Invalid shipping method.') 00322 ); 00323 return $res; 00324 } 00325 $rate = $this->getQuote()->getShippingAddress()->getShippingRateByCode($shippingMethod); 00326 if (!$rate) { 00327 $res = array( 00328 'error' => -1, 00329 'message' => Mage::helper('checkout')->__('Invalid shipping method.') 00330 ); 00331 return $res; 00332 } 00333 $this->getQuote()->getShippingAddress()->setShippingMethod($shippingMethod); 00334 $this->getQuote()->collectTotals()->save(); 00335 00336 $this->getCheckout() 00337 ->setStepData('shipping_method', 'complete', true) 00338 ->setStepData('payment', 'allow', true); 00339 00340 return array(); 00341 }
validateOrder | ( | ) | [protected] |
Definition at line 365 of file Onepage.php.
00366 { 00367 $helper = Mage::helper('checkout'); 00368 if ($this->getQuote()->getIsMultiShipping()) { 00369 Mage::throwException($helper->__('Invalid checkout type.')); 00370 } 00371 00372 if (!$this->getQuote()->isVirtual()) { 00373 $address = $this->getQuote()->getShippingAddress(); 00374 $addressValidation = $address->validate(); 00375 if ($addressValidation !== true) { 00376 Mage::throwException($helper->__('Please check shipping address information.')); 00377 } 00378 $method= $address->getShippingMethod(); 00379 $rate = $address->getShippingRateByCode($method); 00380 if (!$this->getQuote()->isVirtual() && (!$method || !$rate)) { 00381 Mage::throwException($helper->__('Please specify shipping method.')); 00382 } 00383 } 00384 00385 $addressValidation = $this->getQuote()->getBillingAddress()->validate(); 00386 if ($addressValidation !== true) { 00387 Mage::throwException($helper->__('Please check billing address information.')); 00388 } 00389 00390 if (!($this->getQuote()->getPayment()->getMethod())) { 00391 Mage::throwException($helper->__('Please select valid payment method.')); 00392 } 00393 }