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 class Mage_Checkout_Model_Session extends Mage_Core_Model_Session_Abstract
00029 {
00030 const CHECKOUT_STATE_BEGIN = 'begin';
00031 protected $_quote = null;
00032
00033 public function __construct()
00034 {
00035 $this->init('checkout');
00036 }
00037
00038 public function unsetAll()
00039 {
00040 parent::unsetAll();
00041 $this->_quote = null;
00042 }
00043
00044
00045
00046
00047
00048
00049 public function getQuote()
00050 {
00051 if ($this->_quote === null) {
00052 $quote = Mage::getModel('sales/quote')
00053 ->setStoreId(Mage::app()->getStore()->getId());
00054
00055
00056 if ($this->getQuoteId()) {
00057 $quote->load($this->getQuoteId());
00058 if (!$quote->getId()) {
00059 $this->setQuoteId(null);
00060 }
00061 }
00062
00063 $customerSession = Mage::getSingleton('customer/session');
00064
00065 if (!$this->getQuoteId()) {
00066 if ($customerSession->isLoggedIn()) {
00067 $quote->loadByCustomer($customerSession->getCustomer());
00068 $this->setQuoteId($quote->getId());
00069 } else {
00070 $quote->setIsCheckoutCart(true);
00071 Mage::dispatchEvent('checkout_quote_init', array('quote'=>$quote));
00072 }
00073 }
00074
00075 if ($this->getQuoteId()) {
00076 if ($customerSession->isLoggedIn()) {
00077 $quote->setCustomer($customerSession->getCustomer());
00078 }
00079 }
00080
00081 $quote->setStore(Mage::app()->getStore());
00082 $this->_quote = $quote;
00083 }
00084
00085 if (isset($_SERVER['REMOTE_ADDR'])) {
00086 $this->_quote->setRemoteIp($_SERVER['REMOTE_ADDR']);
00087 }
00088 return $this->_quote;
00089 }
00090
00091 protected function _getQuoteIdKey()
00092 {
00093 return 'quote_id_' . Mage::app()->getStore()->getWebsiteId();
00094 }
00095
00096 public function setQuoteId($quoteId)
00097 {
00098 $this->setData($this->_getQuoteIdKey(), $quoteId);
00099 }
00100
00101 public function getQuoteId()
00102 {
00103 return $this->getData($this->_getQuoteIdKey());
00104 }
00105
00106
00107
00108
00109
00110
00111 public function loadCustomerQuote()
00112 {
00113 $customerQuote = Mage::getModel('sales/quote')
00114 ->setStoreId(Mage::app()->getStore()->getId())
00115 ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
00116
00117 if ($this->getQuoteId() != $customerQuote->getId()) {
00118 if ($this->getQuoteId()) {
00119 $customerQuote->merge($this->getQuote())
00120 ->collectTotals()
00121 ->save();
00122 }
00123
00124 $this->setQuoteId($customerQuote->getId());
00125
00126 if ($this->_quote) {
00127 $this->_quote->delete();
00128 }
00129 $this->_quote = $customerQuote;
00130 }
00131 return $this;
00132 }
00133
00134 public function setStepData($step, $data, $value=null)
00135 {
00136 $steps = $this->getSteps();
00137 if (is_null($value)) {
00138 if (is_array($data)) {
00139 $steps[$step] = $data;
00140 }
00141 } else {
00142 if (!isset($steps[$step])) {
00143 $steps[$step] = array();
00144 }
00145 if (is_string($data)) {
00146 $steps[$step][$data] = $value;
00147 }
00148 }
00149 $this->setSteps($steps);
00150
00151 return $this;
00152 }
00153
00154 public function getStepData($step=null, $data=null)
00155 {
00156 $steps = $this->getSteps();
00157 if (is_null($step)) {
00158 return $steps;
00159 }
00160 if (!isset($steps[$step])) {
00161 return false;
00162 }
00163 if (is_null($data)) {
00164 return $steps[$step];
00165 }
00166 if (!is_string($data) || !isset($steps[$step][$data])) {
00167 return false;
00168 }
00169 return $steps[$step][$data];
00170 }
00171
00172 public function clear()
00173 {
00174 Mage::dispatchEvent('checkout_quote_destroy', array('quote'=>$this->getQuote()));
00175 $this->_quote = null;
00176 $this->setQuoteId(null);
00177 $this->setLastSuccessQuoteId(null);
00178 }
00179
00180 public function resetCheckout()
00181 {
00182 $this->setCheckoutState(self::CHECKOUT_STATE_BEGIN);
00183 return $this;
00184 }
00185
00186 public function replaceQuote($quote)
00187 {
00188 $this->_quote = $quote;
00189 $this->setQuoteId($quote->getId());
00190 return $this;
00191 }
00192 }