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_CatalogInventory
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 stock qty abstarct block
30: *
31: * @category Mage
32: * @package Mage_CatalogInventory
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: abstract class Mage_CatalogInventory_Block_Stockqty_Abstract extends Mage_Core_Block_Template
36: {
37: const XML_PATH_STOCK_THRESHOLD_QTY = 'cataloginventory/options/stock_threshold_qty';
38:
39: /**
40: * Retrieve current product object
41: *
42: * @return Mage_Catalog_Model_Product
43: */
44: protected function _getProduct()
45: {
46: return Mage::registry('current_product');
47: }
48:
49: /**
50: * Retrieve current product stock qty
51: *
52: * @return float
53: */
54: public function getStockQty()
55: {
56: if (!$this->hasData('product_stock_qty')) {
57: $qty = 0;
58: if ($stockItem = $this->_getProduct()->getStockItem()) {
59: $qty = (float) $stockItem->getStockQty();
60: }
61: $this->setData('product_stock_qty', $qty);
62: }
63: return $this->getData('product_stock_qty');
64: }
65:
66: /**
67: * Retrieve threshold of qty to display stock qty message
68: *
69: * @return string
70: */
71: public function getThresholdQty()
72: {
73: if (!$this->hasData('threshold_qty')) {
74: $qty = (float) Mage::getStoreConfig(self::XML_PATH_STOCK_THRESHOLD_QTY);
75: $this->setData('threshold_qty', $qty);
76: }
77: return $this->getData('threshold_qty');
78: }
79:
80: /**
81: * Retrieve id of message placeholder in template
82: *
83: * @return string
84: */
85: public function getPlaceholderId()
86: {
87: return 'stock-qty-' . $this->_getProduct()->getId();
88: }
89:
90: /**
91: * Retrieve visibility of stock qty message
92: *
93: * @return bool
94: */
95: public function isMsgVisible()
96: {
97: return ($this->getStockQty() > 0 && $this->getStockQty() <= $this->getThresholdQty());
98: }
99:
100: }
101: