PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/app/code/core/Mage/Checkout/Block/Cart/Sidebar.php

https://github.com/chrisroseuk/magento19_dev
PHP | 310 lines | 156 code | 25 blank | 129 comment | 22 complexity | d2d96db1da22f54d8c7137017a3875e2 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Checkout
  23. * @copyright Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Wishlist sidebar block
  28. *
  29. * @category Mage
  30. * @package Mage_Checkout
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Checkout_Block_Cart_Sidebar extends Mage_Checkout_Block_Cart_Minicart
  34. {
  35. const XML_PATH_CHECKOUT_SIDEBAR_COUNT = 'checkout/sidebar/count';
  36. const XML_PATH_CHECKOUT_MINICART_VISIBLE_ITEMS_COUNT = 'checkout/cart/minicart_visible_items';
  37. /**
  38. * Class constructor
  39. */
  40. public function __construct()
  41. {
  42. parent::__construct();
  43. $this->addItemRender('default', 'checkout/cart_item_renderer', 'checkout/cart/sidebar/default.phtml');
  44. }
  45. /**
  46. * Retrieve count of display recently added items
  47. *
  48. * @return int
  49. */
  50. public function getItemCount()
  51. {
  52. $count = $this->getData('item_count');
  53. if (is_null($count)) {
  54. $count = Mage::getStoreConfig(self::XML_PATH_CHECKOUT_SIDEBAR_COUNT);
  55. $this->setData('item_count', $count);
  56. }
  57. return $count;
  58. }
  59. /**
  60. * Get array of last added items
  61. *
  62. * @param null $count
  63. * @return array
  64. */
  65. public function getRecentItems($count = null)
  66. {
  67. if (!$this->getSummaryCount()) {
  68. return array();
  69. }
  70. if ($count === null) {
  71. $count = $this->getItemCount();
  72. }
  73. return array_slice(array_reverse($this->getItems()), 0, $count);
  74. }
  75. /**
  76. * Get shopping cart subtotal.
  77. *
  78. * It will include tax, if required by config settings.
  79. *
  80. * @param bool $skipTax flag for getting price with tax or not. Ignored in case when we display just subtotal incl.tax
  81. * @return decimal
  82. */
  83. public function getSubtotal($skipTax = true)
  84. {
  85. $subtotal = 0;
  86. $totals = $this->getTotals();
  87. $config = Mage::getSingleton('tax/config');
  88. if (isset($totals['subtotal'])) {
  89. if ($config->displayCartSubtotalBoth()) {
  90. if ($skipTax) {
  91. $subtotal = $totals['subtotal']->getValueExclTax();
  92. } else {
  93. $subtotal = $totals['subtotal']->getValueInclTax();
  94. }
  95. } elseif($config->displayCartSubtotalInclTax()) {
  96. $subtotal = $totals['subtotal']->getValueInclTax();
  97. } else {
  98. $subtotal = $totals['subtotal']->getValue();
  99. if (!$skipTax && isset($totals['tax'])) {
  100. $subtotal+= $totals['tax']->getValue();
  101. }
  102. }
  103. }
  104. return $subtotal;
  105. }
  106. /**
  107. * Get subtotal, including tax.
  108. * Will return > 0 only if appropriate config settings are enabled.
  109. *
  110. * @return decimal
  111. */
  112. public function getSubtotalInclTax()
  113. {
  114. if (!Mage::getSingleton('tax/config')->displayCartSubtotalBoth()) {
  115. return 0;
  116. }
  117. return $this->getSubtotal(false);
  118. }
  119. /**
  120. * Add tax to amount
  121. *
  122. * @param float $price
  123. * @param bool $exclShippingTax
  124. * @return float
  125. */
  126. private function _addTax($price, $exclShippingTax=true) {
  127. $totals = $this->getTotals();
  128. if (isset($totals['tax'])) {
  129. if ($exclShippingTax) {
  130. $price += $totals['tax']->getValue()-$this->_getShippingTaxAmount();
  131. } else {
  132. $price += $totals['tax']->getValue();
  133. }
  134. }
  135. return $price;
  136. }
  137. /**
  138. * Get shipping tax amount
  139. *
  140. * @return float
  141. */
  142. protected function _getShippingTaxAmount()
  143. {
  144. $quote = $this->getCustomQuote() ? $this->getCustomQuote() : $this->getQuote();
  145. return $quote->getShippingAddress()->getShippingTaxAmount();
  146. }
  147. /**
  148. * Get incl/excl tax label
  149. *
  150. * @param bool $flag
  151. * @return string
  152. */
  153. public function getIncExcTax($flag)
  154. {
  155. $text = Mage::helper('tax')->getIncExcText($flag);
  156. return $text ? ' ('.$text.')' : '';
  157. }
  158. /**
  159. * Check if one page checkout is available
  160. *
  161. * @return bool
  162. */
  163. public function isPossibleOnepageCheckout()
  164. {
  165. return $this->helper('checkout')->canOnepageCheckout() && !$this->getQuote()->getHasError();
  166. }
  167. /**
  168. * Get one page checkout page url
  169. *
  170. * @return bool
  171. */
  172. public function getCheckoutUrl()
  173. {
  174. return $this->helper('checkout/url')->getCheckoutUrl();
  175. }
  176. /**
  177. * Define if Shopping Cart Sidebar enabled
  178. *
  179. * @return bool
  180. */
  181. public function getIsNeedToDisplaySideBar()
  182. {
  183. return (bool) Mage::app()->getStore()->getConfig('checkout/sidebar/display');
  184. }
  185. /**
  186. * Return customer quote items
  187. *
  188. * @return array
  189. */
  190. public function getItems()
  191. {
  192. if ($this->getCustomQuote()) {
  193. return $this->getCustomQuote()->getAllVisibleItems();
  194. }
  195. return parent::getItems();
  196. }
  197. /*
  198. * Return totals from custom quote if needed
  199. *
  200. * @return array
  201. */
  202. public function getTotalsCache()
  203. {
  204. if (empty($this->_totals)) {
  205. $quote = $this->getCustomQuote() ? $this->getCustomQuote() : $this->getQuote();
  206. $this->_totals = $quote->getTotals();
  207. }
  208. return $this->_totals;
  209. }
  210. /**
  211. * Get cache key informative items
  212. *
  213. * @return array
  214. */
  215. public function getCacheKeyInfo()
  216. {
  217. $cacheKeyInfo = parent::getCacheKeyInfo();
  218. $cacheKeyInfo['item_renders'] = $this->_serializeRenders();
  219. return $cacheKeyInfo;
  220. }
  221. /**
  222. * Serialize renders
  223. *
  224. * @return string
  225. */
  226. protected function _serializeRenders()
  227. {
  228. $result = array();
  229. foreach ($this->_itemRenders as $type => $renderer) {
  230. $result[] = implode('|', array($type, $renderer['block'], $renderer['template']));
  231. }
  232. return implode('|', $result);
  233. }
  234. /**
  235. * Deserialize renders from string
  236. *
  237. * @param string $renders
  238. * @return Mage_Checkout_Block_Cart_Sidebar
  239. */
  240. public function deserializeRenders($renders)
  241. {
  242. if (!is_string($renders)) {
  243. return $this;
  244. }
  245. $renders = explode('|', $renders);
  246. while (!empty($renders)) {
  247. $template = array_pop($renders);
  248. $block = array_pop($renders);
  249. $type = array_pop($renders);
  250. if (!$template || !$block || !$type) {
  251. continue;
  252. }
  253. $this->addItemRender($type, $block, $template);
  254. }
  255. return $this;
  256. }
  257. /**
  258. * Retrieve block cache tags
  259. *
  260. * @return array
  261. */
  262. public function getCacheTags()
  263. {
  264. $quoteTags = $this->getQuote()->getCacheIdTags();
  265. $items = array();
  266. /** @var $item Mage_Sales_Model_Quote_Item */
  267. foreach ($this->getItems() as $item) {
  268. $items[] = $item->getProduct();
  269. }
  270. return array_merge(
  271. parent::getCacheTags(),
  272. (!$quoteTags)? array() : $quoteTags,
  273. $this->getItemsTags($items)
  274. );
  275. }
  276. /**
  277. * Get form key
  278. *
  279. * @return string
  280. */
  281. public function getFormKey()
  282. {
  283. return Mage::getSingleton('core/session')->getFormKey();
  284. }
  285. }