PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Checkout/Model/Session.php

https://gitlab.com/blingbang2016/shop
PHP | 429 lines | 244 code | 43 blank | 142 comment | 42 complexity | f8abf097841c62f56529f482a406296b MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Checkout
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Checkout_Model_Session extends Mage_Core_Model_Session_Abstract
  27. {
  28. const CHECKOUT_STATE_BEGIN = 'begin';
  29. /**
  30. * Quote instance
  31. *
  32. * @var null|Mage_Sales_Model_Quote
  33. */
  34. protected $_quote;
  35. /**
  36. * Customer instance
  37. *
  38. * @var null|Mage_Customer_Model_Customer
  39. */
  40. protected $_customer;
  41. /**
  42. * Whether load only active quote
  43. *
  44. * @var bool
  45. */
  46. protected $_loadInactive = false;
  47. /**
  48. * Loaded order instance
  49. *
  50. * @var Mage_Sales_Model_Order
  51. */
  52. protected $_order;
  53. /**
  54. * Class constructor. Initialize checkout session namespace
  55. */
  56. public function __construct()
  57. {
  58. $this->init('checkout');
  59. }
  60. /**
  61. * Unset all data associated with object
  62. */
  63. public function unsetAll()
  64. {
  65. parent::unsetAll();
  66. $this->_quote = null;
  67. }
  68. /**
  69. * Set customer instance
  70. *
  71. * @param Mage_Customer_Model_Customer|null $customer
  72. * @return Mage_Checkout_Model_Session
  73. */
  74. public function setCustomer($customer)
  75. {
  76. $this->_customer = $customer;
  77. return $this;
  78. }
  79. /**
  80. * Check whether current session has quote
  81. *
  82. * @return bool
  83. */
  84. public function hasQuote()
  85. {
  86. return isset($this->_quote);
  87. }
  88. /**
  89. * Set quote to be loaded even if inactive
  90. *
  91. * @param bool $load
  92. * @return Mage_Checkout_Model_Session
  93. */
  94. public function setLoadInactive($load = true)
  95. {
  96. $this->_loadInactive = $load;
  97. return $this;
  98. }
  99. /**
  100. * Get checkout quote instance by current session
  101. *
  102. * @return Mage_Sales_Model_Quote
  103. */
  104. public function getQuote()
  105. {
  106. Mage::dispatchEvent('custom_quote_process', array('checkout_session' => $this));
  107. if ($this->_quote === null) {
  108. /** @var $quote Mage_Sales_Model_Quote */
  109. $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
  110. if ($this->getQuoteId()) {
  111. if ($this->_loadInactive) {
  112. $quote->load($this->getQuoteId());
  113. } else {
  114. $quote->loadActive($this->getQuoteId());
  115. }
  116. if ($quote->getId()) {
  117. /**
  118. * If current currency code of quote is not equal current currency code of store,
  119. * need recalculate totals of quote. It is possible if customer use currency switcher or
  120. * store switcher.
  121. */
  122. if ($quote->getQuoteCurrencyCode() != Mage::app()->getStore()->getCurrentCurrencyCode()) {
  123. $quote->setStore(Mage::app()->getStore());
  124. $quote->collectTotals()->save();
  125. /*
  126. * We mast to create new quote object, because collectTotals()
  127. * can to create links with other objects.
  128. */
  129. $quote = Mage::getModel('sales/quote')->setStoreId(Mage::app()->getStore()->getId());
  130. $quote->load($this->getQuoteId());
  131. }
  132. } else {
  133. $this->setQuoteId(null);
  134. }
  135. }
  136. $customerSession = Mage::getSingleton('customer/session');
  137. if (!$this->getQuoteId()) {
  138. if ($customerSession->isLoggedIn() || $this->_customer) {
  139. $customer = ($this->_customer) ? $this->_customer : $customerSession->getCustomer();
  140. $quote->loadByCustomer($customer);
  141. $this->setQuoteId($quote->getId());
  142. } else {
  143. $quote->setIsCheckoutCart(true);
  144. Mage::dispatchEvent('checkout_quote_init', array('quote'=>$quote));
  145. }
  146. }
  147. if ($this->getQuoteId()) {
  148. if ($customerSession->isLoggedIn() || $this->_customer) {
  149. $customer = ($this->_customer) ? $this->_customer : $customerSession->getCustomer();
  150. $quote->setCustomer($customer);
  151. }
  152. }
  153. $quote->setStore(Mage::app()->getStore());
  154. $this->_quote = $quote;
  155. }
  156. if ($remoteAddr = Mage::helper('core/http')->getRemoteAddr()) {
  157. $this->_quote->setRemoteIp($remoteAddr);
  158. $xForwardIp = Mage::app()->getRequest()->getServer('HTTP_X_FORWARDED_FOR');
  159. $this->_quote->setXForwardedFor($xForwardIp);
  160. }
  161. return $this->_quote;
  162. }
  163. protected function _getQuoteIdKey()
  164. {
  165. return 'quote_id_' . Mage::app()->getStore()->getWebsiteId();
  166. }
  167. public function setQuoteId($quoteId)
  168. {
  169. $this->setData($this->_getQuoteIdKey(), $quoteId);
  170. }
  171. public function getQuoteId()
  172. {
  173. return $this->getData($this->_getQuoteIdKey());
  174. }
  175. /**
  176. * Load data for customer quote and merge with current quote
  177. *
  178. * @return Mage_Checkout_Model_Session
  179. */
  180. public function loadCustomerQuote()
  181. {
  182. if (!Mage::getSingleton('customer/session')->getCustomerId()) {
  183. return $this;
  184. }
  185. Mage::dispatchEvent('load_customer_quote_before', array('checkout_session' => $this));
  186. $customerQuote = Mage::getModel('sales/quote')
  187. ->setStoreId(Mage::app()->getStore()->getId())
  188. ->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
  189. if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
  190. if ($this->getQuoteId()) {
  191. $customerQuote->merge($this->getQuote())
  192. ->collectTotals()
  193. ->save();
  194. }
  195. $this->setQuoteId($customerQuote->getId());
  196. if ($this->_quote) {
  197. $this->_quote->delete();
  198. }
  199. $this->_quote = $customerQuote;
  200. } else {
  201. $this->getQuote()->getBillingAddress();
  202. $this->getQuote()->getShippingAddress();
  203. $this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
  204. ->setTotalsCollectedFlag(false)
  205. ->collectTotals()
  206. ->save();
  207. }
  208. return $this;
  209. }
  210. public function setStepData($step, $data, $value=null)
  211. {
  212. $steps = $this->getSteps();
  213. if (is_null($value)) {
  214. if (is_array($data)) {
  215. $steps[$step] = $data;
  216. }
  217. } else {
  218. if (!isset($steps[$step])) {
  219. $steps[$step] = array();
  220. }
  221. if (is_string($data)) {
  222. $steps[$step][$data] = $value;
  223. }
  224. }
  225. $this->setSteps($steps);
  226. return $this;
  227. }
  228. public function getStepData($step=null, $data=null)
  229. {
  230. $steps = $this->getSteps();
  231. if (is_null($step)) {
  232. return $steps;
  233. }
  234. if (!isset($steps[$step])) {
  235. return false;
  236. }
  237. if (is_null($data)) {
  238. return $steps[$step];
  239. }
  240. if (!is_string($data) || !isset($steps[$step][$data])) {
  241. return false;
  242. }
  243. return $steps[$step][$data];
  244. }
  245. /**
  246. * Retrieves list of all saved additional messages for different instances (e.g. quote items) in checkout session
  247. * Returned: array(itemKey => messageCollection, ...)
  248. * where itemKey is a unique hash (e.g 'quote_item17') to distinguish item messages among message collections
  249. *
  250. * @param bool $clear
  251. *
  252. * @return array
  253. */
  254. public function getAdditionalMessages($clear = false)
  255. {
  256. $additionalMessages = $this->getData('additional_messages');
  257. if (!$additionalMessages) {
  258. return array();
  259. }
  260. if ($clear) {
  261. $this->setData('additional_messages', null);
  262. }
  263. return $additionalMessages;
  264. }
  265. /**
  266. * Retrieves list of item additional messages
  267. * itemKey is a unique hash (e.g 'quote_item17') to distinguish item messages among message collections
  268. *
  269. * @param string $itemKey
  270. * @param bool $clear
  271. *
  272. * @return null|Mage_Core_Model_Message_Collection
  273. */
  274. public function getItemAdditionalMessages($itemKey, $clear = false)
  275. {
  276. $allMessages = $this->getAdditionalMessages();
  277. if (!isset($allMessages[$itemKey])) {
  278. return null;
  279. }
  280. $messages = $allMessages[$itemKey];
  281. if ($clear) {
  282. unset($allMessages[$itemKey]);
  283. $this->setAdditionalMessages($allMessages);
  284. }
  285. return $messages;
  286. }
  287. /**
  288. * Adds new message in this session to a list of additional messages for some item
  289. * itemKey is a unique hash (e.g 'quote_item17') to distinguish item messages among message collections
  290. *
  291. * @param string $itemKey
  292. * @param Mage_Core_Model_Message $message
  293. *
  294. * @return Mage_Checkout_Model_Session
  295. */
  296. public function addItemAdditionalMessage($itemKey, $message)
  297. {
  298. $allMessages = $this->getAdditionalMessages();
  299. if (!isset($allMessages[$itemKey])) {
  300. $allMessages[$itemKey] = Mage::getModel('core/message_collection');
  301. }
  302. $allMessages[$itemKey]->add($message);
  303. $this->setAdditionalMessages($allMessages);
  304. return $this;
  305. }
  306. /**
  307. * Retrieves list of quote item messages
  308. * @param int $itemId
  309. * @param bool $clear
  310. *
  311. * @return null|Mage_Core_Model_Message_Collection
  312. */
  313. public function getQuoteItemMessages($itemId, $clear = false)
  314. {
  315. return $this->getItemAdditionalMessages('quote_item' . $itemId, $clear);
  316. }
  317. /**
  318. * Adds new message to a list of quote item messages, saved in this session
  319. *
  320. * @param int $itemId
  321. * @param Mage_Core_Model_Message $message
  322. *
  323. * @return Mage_Checkout_Model_Session
  324. */
  325. function addQuoteItemMessage($itemId, $message)
  326. {
  327. return $this->addItemAdditionalMessage('quote_item' . $itemId, $message);
  328. }
  329. public function clear()
  330. {
  331. Mage::dispatchEvent('checkout_quote_destroy', array('quote'=>$this->getQuote()));
  332. $this->_quote = null;
  333. $this->setQuoteId(null);
  334. $this->setLastSuccessQuoteId(null);
  335. }
  336. /**
  337. * Clear misc checkout parameters
  338. */
  339. public function clearHelperData()
  340. {
  341. $this->setLastBillingAgreementId(null)
  342. ->setRedirectUrl(null)
  343. ->setLastOrderId(null)
  344. ->setLastRealOrderId(null)
  345. ->setLastRecurringProfileIds(null)
  346. ->setAdditionalMessages(null)
  347. ;
  348. }
  349. public function resetCheckout()
  350. {
  351. $this->setCheckoutState(self::CHECKOUT_STATE_BEGIN);
  352. return $this;
  353. }
  354. public function replaceQuote($quote)
  355. {
  356. $this->_quote = $quote;
  357. $this->setQuoteId($quote->getId());
  358. return $this;
  359. }
  360. /**
  361. * Get order instance based on last order ID
  362. *
  363. * @return Mage_Sales_Model_Order
  364. */
  365. public function getLastRealOrder()
  366. {
  367. $orderId = $this->getLastRealOrderId();
  368. if ($this->_order !== null && $orderId == $this->_order->getIncrementId()) {
  369. return $this->_order;
  370. }
  371. $this->_order = $this->_getOrderModel();
  372. if ($orderId) {
  373. $this->_order->loadByIncrementId($orderId);
  374. }
  375. return $this->_order;
  376. }
  377. /**
  378. * Get order model
  379. *
  380. * @return Mage_Sales_Model_Order
  381. */
  382. protected function _getOrderModel()
  383. {
  384. return Mage::getModel('sales/order');
  385. }
  386. }