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_Catalog
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: /**
29: * Product price block
30: *
31: * @category Mage
32: * @package Mage_Catalog
33: */
34: class Mage_Catalog_Block_Product_Price extends Mage_Core_Block_Template
35: {
36: protected $_priceDisplayType = null;
37: protected $_idSuffix = '';
38:
39: /**
40: * Retrieve product
41: *
42: * @return Mage_Catalog_Model_Product
43: */
44: public function getProduct()
45: {
46: $product = $this->_getData('product');
47: if (!$product) {
48: $product = Mage::registry('product');
49: }
50: return $product;
51: }
52:
53: public function getDisplayMinimalPrice()
54: {
55: return $this->_getData('display_minimal_price');
56: }
57:
58: public function setIdSuffix($idSuffix)
59: {
60: $this->_idSuffix = $idSuffix;
61: return $this;
62: }
63:
64: public function getIdSuffix()
65: {
66: return $this->_idSuffix;
67: }
68:
69: /**
70: * Get tier prices (formatted)
71: *
72: * @param Mage_Catalog_Model_Product $product
73: * @return array
74: */
75: public function getTierPrices($product = null)
76: {
77: if (is_null($product)) {
78: $product = $this->getProduct();
79: }
80: $prices = $product->getFormatedTierPrice();
81:
82: $res = array();
83: if (is_array($prices)) {
84: foreach ($prices as $price) {
85: $price['price_qty'] = $price['price_qty'] * 1;
86:
87: $productPrice = $product->getPrice();
88: if ($product->getPrice() != $product->getFinalPrice()) {
89: $productPrice = $product->getFinalPrice();
90: }
91:
92: // Group price must be used for percent calculation if it is lower
93: $groupPrice = $product->getGroupPrice();
94: if ($productPrice > $groupPrice) {
95: $productPrice = $groupPrice;
96: }
97:
98: if ($price['price'] < $productPrice) {
99: $price['savePercent'] = ceil(100 - ((100 / $productPrice) * $price['price']));
100:
101: $tierPrice = Mage::app()->getStore()->convertPrice(
102: Mage::helper('tax')->getPrice($product, $price['website_price'])
103: );
104: $price['formated_price'] = Mage::app()->getStore()->formatPrice($tierPrice);
105: $price['formated_price_incl_tax'] = Mage::app()->getStore()->formatPrice(
106: Mage::app()->getStore()->convertPrice(
107: Mage::helper('tax')->getPrice($product, $price['website_price'], true)
108: )
109: );
110:
111: if (Mage::helper('catalog')->canApplyMsrp($product)) {
112: $oldPrice = $product->getFinalPrice();
113: $product->setPriceCalculation(false);
114: $product->setPrice($tierPrice);
115: $product->setFinalPrice($tierPrice);
116:
117: $this->getLayout()->getBlock('product.info')->getPriceHtml($product);
118: $product->setPriceCalculation(true);
119:
120: $price['real_price_html'] = $product->getRealPriceHtml();
121: $product->setFinalPrice($oldPrice);
122: }
123:
124: $res[] = $price;
125: }
126: }
127: }
128:
129: return $res;
130: }
131:
132: /**
133: * Retrieve url for direct adding product to cart
134: *
135: * @param Mage_Catalog_Model_Product $product
136: * @param array $additional
137: * @return string
138: */
139: public function getAddToCartUrl($product, $additional = array())
140: {
141: return $this->helper('checkout/cart')->getAddUrl($product, $additional);
142: }
143:
144: /**
145: * Prevent displaying if the price is not available
146: *
147: * @return string
148: */
149: protected function _toHtml()
150: {
151: if (!$this->getProduct() || $this->getProduct()->getCanShowPrice() === false) {
152: return '';
153: }
154: return parent::_toHtml();
155: }
156:
157: /**
158: * Get Product Price valid JS string
159: *
160: * @param Mage_Catalog_Model_Product $product
161: * @return string
162: */
163: public function getRealPriceJs($product)
164: {
165: $html = $this->hasRealPriceHtml() ? $this->getRealPriceHtml() : $product->getRealPriceHtml();
166: return Mage::helper('core')->jsonEncode($html);
167: }
168: }
169: