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) 2012 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: /**
28: * Shopping cart abstract block
29: *
30: * @category Mage
31: * @package Mage_Checkout
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_Checkout_Block_Cart_Abstract extends Mage_Core_Block_Template
35: {
36: protected $_customer = null;
37: protected $_checkout = null;
38: protected $_quote = null;
39:
40: protected $_totals;
41: protected $_itemRenders = array();
42:
43: public function __construct()
44: {
45: parent::__construct();
46: $this->addItemRender('default', 'checkout/cart_item_renderer', 'checkout/cart/item/default.phtml');
47: }
48:
49: /**
50: * Add renderer for item product type
51: *
52: * @param string $productType
53: * @param string $blockType
54: * @param string $template
55: * @return Mage_Checkout_Block_Cart_Abstract
56: */
57: public function addItemRender($productType, $blockType, $template)
58: {
59: $this->_itemRenders[$productType] = array(
60: 'block' => $blockType,
61: 'template' => $template,
62: 'blockInstance' => null
63: );
64: return $this;
65: }
66:
67: /**
68: * Get renderer information by product type code
69: *
70: * @deprecated please use getItemRendererInfo() method instead
71: * @see getItemRendererInfo()
72: * @param string $type
73: * @return array
74: */
75: public function getItemRender($type)
76: {
77: return $this->getItemRendererInfo($type);
78: }
79:
80: /**
81: * Get renderer information by product type code
82: *
83: * @param string $type
84: * @return array
85: */
86: public function getItemRendererInfo($type)
87: {
88: if (isset($this->_itemRenders[$type])) {
89: return $this->_itemRenders[$type];
90: }
91: return $this->_itemRenders['default'];
92: }
93:
94: /**
95: * Get renderer block instance by product type code
96: *
97: * @param string $type
98: * @return array
99: */
100: public function getItemRenderer($type)
101: {
102: if (!isset($this->_itemRenders[$type])) {
103: $type = 'default';
104: }
105: if (is_null($this->_itemRenders[$type]['blockInstance'])) {
106: $this->_itemRenders[$type]['blockInstance'] = $this->getLayout()
107: ->createBlock($this->_itemRenders[$type]['block'])
108: ->setTemplate($this->_itemRenders[$type]['template'])
109: ->setRenderedBlock($this);
110: }
111:
112: return $this->_itemRenders[$type]['blockInstance'];
113: }
114:
115:
116: /**
117: * Get logged in customer
118: *
119: * @return Mage_Customer_Model_Customer
120: */
121: public function getCustomer()
122: {
123: if (null === $this->_customer) {
124: $this->_customer = Mage::getSingleton('customer/session')->getCustomer();
125: }
126: return $this->_customer;
127: }
128:
129: /**
130: * Get checkout session
131: *
132: * @return Mage_Checkout_Model_Session
133: */
134: public function getCheckout()
135: {
136: if (null === $this->_checkout) {
137: $this->_checkout = Mage::getSingleton('checkout/session');
138: }
139: return $this->_checkout;
140: }
141:
142: /**
143: * Get active quote
144: *
145: * @return Mage_Sales_Model_Quote
146: */
147: public function getQuote()
148: {
149: if (null === $this->_quote) {
150: $this->_quote = $this->getCheckout()->getQuote();
151: }
152: return $this->_quote;
153: }
154:
155: /**
156: * Get all cart items
157: *
158: * @return array
159: */
160: public function getItems()
161: {
162: return $this->getQuote()->getAllVisibleItems();
163: }
164:
165: /**
166: * Get item row html
167: *
168: * @param Mage_Sales_Model_Quote_Item $item
169: * @return string
170: */
171: public function getItemHtml(Mage_Sales_Model_Quote_Item $item)
172: {
173: $renderer = $this->getItemRenderer($item->getProductType())->setItem($item);
174: return $renderer->toHtml();
175: }
176:
177: public function getTotals()
178: {
179: return $this->getTotalsCache();
180: }
181:
182: public function getTotalsCache()
183: {
184: if (empty($this->_totals)) {
185: $this->_totals = $this->getQuote()->getTotals();
186: }
187: return $this->_totals;
188: }
189:
190: /**
191: * Check if can apply msrp to totals
192: *
193: * @return bool
194: */
195: public function canApplyMsrp()
196: {
197: if (!$this->getQuote()->hasCanApplyMsrp() && Mage::helper('catalog')->isMsrpEnabled()) {
198: $this->getQuote()->collectTotals();
199: }
200: return $this->getQuote()->getCanApplyMsrp();
201: }
202: }
203: