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: class Mage_Checkout_Model_Session extends Mage_Core_Model_Session_Abstract
29: {
30: const CHECKOUT_STATE_BEGIN = 'begin';
31:
32: 33: 34: 35: 36:
37: protected $_quote;
38:
39: 40: 41: 42: 43:
44: protected $_customer;
45:
46: 47: 48: 49: 50:
51: protected $_loadInactive = false;
52:
53: 54: 55:
56: public function __construct()
57: {
58: $this->init('checkout');
59: }
60:
61: 62: 63:
64: public function unsetAll()
65: {
66: parent::unsetAll();
67: $this->_quote = null;
68: }
69:
70: 71: 72: 73: 74: 75:
76: public function setCustomer($customer)
77: {
78: $this->_customer = $customer;
79: return $this;
80: }
81:
82: 83: 84: 85: 86:
87: public function hasQuote()
88: {
89: return isset($this->_quote);
90: }
91:
92: 93: 94: 95: 96: 97:
98: public function setLoadInactive($load = true)
99: {
100: $this->_loadInactive = $load;
101: return $this;
102: }
103:
104: 105: 106: 107: 108:
109: public function getQuote()
110: {
111: Mage::dispatchEvent('custom_quote_process', array('checkout_session' => $this));
112:
113: if ($this->_quote === null) {
114:
115: $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
116: if ($this->getQuoteId()) {
117: if ($this->_loadInactive) {
118: $quote->load($this->getQuoteId());
119: } else {
120: $quote->loadActive($this->getQuoteId());
121: }
122: if ($quote->getId()) {
123: 124: 125: 126: 127:
128: if ($quote->getQuoteCurrencyCode() != Mage::app()->getStore()->getCurrentCurrencyCode()) {
129: $quote->setStore(Mage::app()->getStore());
130: $quote->collectTotals()->save();
131: 132: 133: 134:
135: $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
136: $quote->load($this->getQuoteId());
137: }
138: } else {
139: $this->setQuoteId(null);
140: }
141: }
142:
143: $customerSession = Mage::getSingleton('customer/session');
144:
145: if (!$this->getQuoteId()) {
146: if ($customerSession->isLoggedIn() || $this->_customer) {
147: $customer = ($this->_customer) ? $this->_customer : $customerSession->getCustomer();
148: $quote->loadByCustomer($customer);
149: $this->setQuoteId($quote->getId());
150: } else {
151: $quote->setIsCheckoutCart(true);
152: Mage::dispatchEvent('checkout_quote_init', array('quote'=>$quote));
153: }
154: }
155:
156: if ($this->getQuoteId()) {
157: if ($customerSession->isLoggedIn() || $this->_customer) {
158: $customer = ($this->_customer) ? $this->_customer : $customerSession->getCustomer();
159: $quote->setCustomer($customer);
160: }
161: }
162:
163: $quote->setStore(Mage::app()->getStore());
164: $this->_quote = $quote;
165: }
166:
167: if ($remoteAddr = Mage::helper('core/http')->getRemoteAddr()) {
168: $this->_quote->setRemoteIp($remoteAddr);
169: $xForwardIp = Mage::app()->getRequest()->getServer('HTTP_X_FORWARDED_FOR');
170: $this->_quote->setXForwardedFor($xForwardIp);
171: }
172: return $this->_quote;
173: }
174:
175: protected function _getQuoteIdKey()
176: {
177: return 'quote_id_' . Mage::app()->getStore()->getWebsiteId();
178: }
179:
180: public function setQuoteId($quoteId)
181: {
182: $this->setData($this->_getQuoteIdKey(), $quoteId);
183: }
184:
185: public function getQuoteId()
186: {
187: return $this->getData($this->_getQuoteIdKey());
188: }
189:
190: 191: 192: 193: 194:
195: public function loadCustomerQuote()
196: {
197: if (!Mage::getSingleton('customer/session')->getCustomerId()) {
198: return $this;
199: }
200:
201: Mage::dispatchEvent('load_customer_quote_before', array('checkout_session' => $this));
202:
203: $customerQuote = Mage::getModel('sales/quote')
204: ->setStoreId(Mage::app()->getStore()->getId())
205: ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
206:
207: if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
208: if ($this->getQuoteId()) {
209: $customerQuote->merge($this->getQuote())
210: ->collectTotals()
211: ->save();
212: }
213:
214: $this->setQuoteId($customerQuote->getId());
215:
216: if ($this->_quote) {
217: $this->_quote->delete();
218: }
219: $this->_quote = $customerQuote;
220: } else {
221: $this->getQuote()->getBillingAddress();
222: $this->getQuote()->getShippingAddress();
223: $this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
224: ->setTotalsCollectedFlag(false)
225: ->collectTotals()
226: ->save();
227: }
228: return $this;
229: }
230:
231: public function setStepData($step, $data, $value=null)
232: {
233: $steps = $this->getSteps();
234: if (is_null($value)) {
235: if (is_array($data)) {
236: $steps[$step] = $data;
237: }
238: } else {
239: if (!isset($steps[$step])) {
240: $steps[$step] = array();
241: }
242: if (is_string($data)) {
243: $steps[$step][$data] = $value;
244: }
245: }
246: $this->setSteps($steps);
247:
248: return $this;
249: }
250:
251: public function getStepData($step=null, $data=null)
252: {
253: $steps = $this->getSteps();
254: if (is_null($step)) {
255: return $steps;
256: }
257: if (!isset($steps[$step])) {
258: return false;
259: }
260: if (is_null($data)) {
261: return $steps[$step];
262: }
263: if (!is_string($data) || !isset($steps[$step][$data])) {
264: return false;
265: }
266: return $steps[$step][$data];
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276: 277:
278: public function getAdditionalMessages($clear = false)
279: {
280: $additionalMessages = $this->getData('additional_messages');
281: if (!$additionalMessages) {
282: return array();
283: }
284: if ($clear) {
285: $this->setData('additional_messages', null);
286: }
287: return $additionalMessages;
288: }
289:
290: 291: 292: 293: 294: 295: 296: 297: 298:
299: public function getItemAdditionalMessages($itemKey, $clear = false)
300: {
301: $allMessages = $this->getAdditionalMessages();
302: if (!isset($allMessages[$itemKey])) {
303: return null;
304: }
305:
306: $messages = $allMessages[$itemKey];
307: if ($clear) {
308: unset($allMessages[$itemKey]);
309: $this->setAdditionalMessages($allMessages);
310: }
311: return $messages;
312: }
313:
314: 315: 316: 317: 318: 319: 320: 321: 322:
323: public function addItemAdditionalMessage($itemKey, $message)
324: {
325: $allMessages = $this->getAdditionalMessages();
326: if (!isset($allMessages[$itemKey])) {
327: $allMessages[$itemKey] = Mage::getModel('core/message_collection');
328: }
329: $allMessages[$itemKey]->add($message);
330: $this->setAdditionalMessages($allMessages);
331:
332: return $this;
333: }
334:
335: 336: 337: 338: 339: 340: 341:
342: public function getQuoteItemMessages($itemId, $clear = false)
343: {
344: return $this->getItemAdditionalMessages('quote_item' . $itemId, $clear);
345: }
346:
347: 348: 349: 350: 351: 352: 353: 354:
355: function addQuoteItemMessage($itemId, $message)
356: {
357: return $this->addItemAdditionalMessage('quote_item' . $itemId, $message);
358: }
359:
360: public function clear()
361: {
362: Mage::dispatchEvent('checkout_quote_destroy', array('quote'=>$this->getQuote()));
363: $this->_quote = null;
364: $this->setQuoteId(null);
365: $this->setLastSuccessQuoteId(null);
366: }
367:
368: 369: 370:
371: public function clearHelperData()
372: {
373: $this->setLastBillingAgreementId(null)
374: ->setRedirectUrl(null)
375: ->setLastOrderId(null)
376: ->setLastRealOrderId(null)
377: ->setLastRecurringProfileIds(null)
378: ->setAdditionalMessages(null)
379: ;
380: }
381:
382: public function resetCheckout()
383: {
384: $this->setCheckoutState(self::CHECKOUT_STATE_BEGIN);
385: return $this;
386: }
387:
388: public function replaceQuote($quote)
389: {
390: $this->_quote = $quote;
391: $this->setQuoteId($quote->getId());
392: return $this;
393: }
394: }
395: