PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle/Option.php

https://bitbucket.org/dnejedly/eaparts
PHP | 277 lines | 152 code | 26 blank | 99 comment | 32 complexity | a4bbeffe12ee69d3cd72591e63b0be9b 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@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_Bundle
  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. * Bundle option renderer
  28. *
  29. * @category Mage
  30. * @package Mage_Bundle
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option extends Mage_Bundle_Block_Catalog_Product_Price
  34. {
  35. /**
  36. * Store preconfigured options
  37. *
  38. * @var int|array|string
  39. */
  40. protected $_selectedOptions = null;
  41. /**
  42. * Show if option has a single selection
  43. *
  44. * @var bool
  45. */
  46. protected $_showSingle = null;
  47. /**
  48. * Check if option has a single selection
  49. *
  50. * @return bool
  51. */
  52. protected function _showSingle()
  53. {
  54. if (is_null($this->_showSingle)) {
  55. $_option = $this->getOption();
  56. $_selections = $_option->getSelections();
  57. $this->_showSingle = (count($_selections) == 1 && $_option->getRequired());
  58. }
  59. return $this->_showSingle;
  60. }
  61. /**
  62. * Retrieve default values for template
  63. *
  64. * @return array
  65. */
  66. protected function _getDefaultValues()
  67. {
  68. $_option = $this->getOption();
  69. $_default = $_option->getDefaultSelection();
  70. $_selections = $_option->getSelections();
  71. $selectedOptions = $this->_getSelectedOptions();
  72. $inPreConfigured = $this->getProduct()->hasPreconfiguredValues()
  73. && $this->getProduct()->getPreconfiguredValues()
  74. ->getData('bundle_option_qty/' . $_option->getId());
  75. if (empty($selectedOptions) && $_default) {
  76. $_defaultQty = $_default->getSelectionQty()*1;
  77. $_canChangeQty = $_default->getSelectionCanChangeQty();
  78. } elseif (!$inPreConfigured && $selectedOptions && is_numeric($selectedOptions)) {
  79. $selectedSelection = $_option->getSelectionById($selectedOptions);
  80. $_defaultQty = $selectedSelection->getSelectionQty()*1;
  81. $_canChangeQty = $selectedSelection->getSelectionCanChangeQty();
  82. } elseif (!$this->_showSingle() || $inPreConfigured) {
  83. $_defaultQty = $this->_getSelectedQty();
  84. $_canChangeQty = (bool)$_defaultQty;
  85. } else {
  86. $_defaultQty = $_selections[0]->getSelectionQty()*1;
  87. $_canChangeQty = $_selections[0]->getSelectionCanChangeQty();
  88. }
  89. return array($_defaultQty, $_canChangeQty);
  90. }
  91. /**
  92. * Collect selected options
  93. *
  94. * @return void
  95. */
  96. protected function _getSelectedOptions()
  97. {
  98. if (is_null($this->_selectedOptions)) {
  99. $this->_selectedOptions = array();
  100. $option = $this->getOption();
  101. if ($this->getProduct()->hasPreconfiguredValues()) {
  102. $configValue = $this->getProduct()->getPreconfiguredValues()
  103. ->getData('bundle_option/' . $option->getId());
  104. if ($configValue) {
  105. $this->_selectedOptions = $configValue;
  106. } elseif (!$option->getRequired()) {
  107. $this->_selectedOptions = 'None';
  108. }
  109. }
  110. }
  111. return $this->_selectedOptions;
  112. }
  113. /**
  114. * Define if selection is selected
  115. *
  116. * @param Mage_Catalog_Model_Product $selection
  117. * @return bool
  118. */
  119. protected function _isSelected($selection)
  120. {
  121. $selectedOptions = $this->_getSelectedOptions();
  122. if (is_numeric($selectedOptions)) {
  123. return ($selection->getSelectionId() == $this->_getSelectedOptions());
  124. } elseif (is_array($selectedOptions) && !empty($selectedOptions)) {
  125. return in_array($selection->getSelectionId(), $this->_getSelectedOptions());
  126. } elseif ($selectedOptions == 'None') {
  127. return false;
  128. } else {
  129. return ($selection->getIsDefault() && $selection->isSaleable());
  130. }
  131. }
  132. /**
  133. * Retrieve selected option qty
  134. *
  135. * @return int
  136. */
  137. protected function _getSelectedQty()
  138. {
  139. if ($this->getProduct()->hasPreconfiguredValues()) {
  140. $selectedQty = (float)$this->getProduct()->getPreconfiguredValues()
  141. ->getData('bundle_option_qty/' . $this->getOption()->getId());
  142. if ($selectedQty < 0) {
  143. $selectedQty = 0;
  144. }
  145. } else {
  146. $selectedQty = 0;
  147. }
  148. return $selectedQty;
  149. }
  150. /**
  151. * Get product model
  152. *
  153. * @return Mage_Catalog_Model_Product
  154. */
  155. public function getProduct()
  156. {
  157. if (!$this->hasData('product')) {
  158. $this->setData('product', Mage::registry('current_product'));
  159. }
  160. return $this->getData('product');
  161. }
  162. public function getSelectionQtyTitlePrice($_selection, $includeContainer = true)
  163. {
  164. $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection);
  165. $this->setFormatProduct($_selection);
  166. $priceTitle = $_selection->getSelectionQty()*1 . ' x ' . $this->escapeHtml($_selection->getName());
  167. $priceTitle .= ' &nbsp; ' . ($includeContainer ? '<span class="price-notice">' : '')
  168. . '+' . $this->formatPriceString($price, $includeContainer)
  169. . ($includeContainer ? '</span>' : '');
  170. return $priceTitle;
  171. }
  172. /**
  173. * Get price for selection product
  174. *
  175. * @param Mage_Catalog_Model_Product $_selection
  176. * @return int|float
  177. */
  178. public function getSelectionPrice($_selection)
  179. {
  180. $price = 0;
  181. $store = $this->getProduct()->getStore();
  182. if ($_selection) {
  183. $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection);
  184. if (is_numeric($price)) {
  185. $price = $this->helper('core')->currencyByStore($price, $store, false);
  186. }
  187. }
  188. return is_numeric($price) ? $price : 0;
  189. }
  190. /**
  191. * Get title price for selection product
  192. *
  193. * @param Mage_Catalog_Model_Product $_selection
  194. * @param bool $includeContainer
  195. * @return string
  196. */
  197. public function getSelectionTitlePrice($_selection, $includeContainer = true)
  198. {
  199. $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection, 1);
  200. $this->setFormatProduct($_selection);
  201. $priceTitle = $this->escapeHtml($_selection->getName());
  202. $priceTitle .= ' &nbsp; ' . ($includeContainer ? '<span class="price-notice">' : '')
  203. . '+' . $this->formatPriceString($price, $includeContainer)
  204. . ($includeContainer ? '</span>' : '');
  205. return $priceTitle;
  206. }
  207. /**
  208. * Set JS validation container for element
  209. *
  210. * @param int $elementId
  211. * @param int $containerId
  212. * @return string
  213. */
  214. public function setValidationContainer($elementId, $containerId)
  215. {
  216. return '<script type="text/javascript">
  217. $(\'' . $elementId . '\').advaiceContainer = \'' . $containerId . '\';
  218. $(\'' . $elementId . '\').callbackFunction = \'bundle.validationCallback\';
  219. </script>';
  220. }
  221. /**
  222. * Format price string
  223. *
  224. * @param float $price
  225. * @param bool $includeContainer
  226. * @return string
  227. */
  228. public function formatPriceString($price, $includeContainer = true)
  229. {
  230. $taxHelper = Mage::helper('tax');
  231. $coreHelper = $this->helper('core');
  232. $currentProduct = $this->getProduct();
  233. if ($currentProduct->getPriceType() == Mage_Bundle_Model_Product_Price::PRICE_TYPE_DYNAMIC
  234. && $this->getFormatProduct()
  235. ) {
  236. $product = $this->getFormatProduct();
  237. } else {
  238. $product = $currentProduct;
  239. }
  240. $priceTax = $taxHelper->getPrice($product, $price);
  241. $priceIncTax = $taxHelper->getPrice($product, $price, true);
  242. $formated = $coreHelper->currencyByStore($priceTax, $product->getStore(), true, $includeContainer);
  243. if ($taxHelper->displayBothPrices() && $priceTax != $priceIncTax) {
  244. $formated .=
  245. ' (+' .
  246. $coreHelper->currencyByStore($priceIncTax, $product->getStore(), true, $includeContainer) .
  247. ' ' . $this->__('Incl. Tax') . ')';
  248. }
  249. return $formated;
  250. }
  251. }