PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/blingbang2016/shop
PHP | 284 lines | 152 code | 26 blank | 106 comment | 32 complexity | 5063d4fe519483fe11581e326085dfe5 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Bundle
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.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. /**
  163. * Returns the formatted string for the quantity chosen for the given selection
  164. *
  165. * @param Mage_Catalog_Model_Proudct $_selection
  166. * @param bool $includeContainer
  167. * @return string
  168. */
  169. public function getSelectionQtyTitlePrice($_selection, $includeContainer = true)
  170. {
  171. $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection);
  172. $this->setFormatProduct($_selection);
  173. $priceTitle = $_selection->getSelectionQty() * 1 . ' x ' . $this->escapeHtml($_selection->getName());
  174. $priceTitle .= ' &nbsp; ' . ($includeContainer ? '<span class="price-notice">' : '')
  175. . '+' . $this->formatPriceString($price, $includeContainer)
  176. . ($includeContainer ? '</span>' : '');
  177. return $priceTitle;
  178. }
  179. /**
  180. * Get price for selection product
  181. *
  182. * @param Mage_Catalog_Model_Product $_selection
  183. * @return int|float
  184. */
  185. public function getSelectionPrice($_selection)
  186. {
  187. $price = 0;
  188. $store = $this->getProduct()->getStore();
  189. if ($_selection) {
  190. $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection);
  191. if (is_numeric($price)) {
  192. $price = $this->helper('core')->currencyByStore($price, $store, false);
  193. }
  194. }
  195. return is_numeric($price) ? $price : 0;
  196. }
  197. /**
  198. * Get title price for selection product
  199. *
  200. * @param Mage_Catalog_Model_Product $_selection
  201. * @param bool $includeContainer
  202. * @return string
  203. */
  204. public function getSelectionTitlePrice($_selection, $includeContainer = true)
  205. {
  206. $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection, 1);
  207. $this->setFormatProduct($_selection);
  208. $priceTitle = $this->escapeHtml($_selection->getName());
  209. $priceTitle .= ' &nbsp; ' . ($includeContainer ? '<span class="price-notice">' : '')
  210. . '+' . $this->formatPriceString($price, $includeContainer)
  211. . ($includeContainer ? '</span>' : '');
  212. return $priceTitle;
  213. }
  214. /**
  215. * Set JS validation container for element
  216. *
  217. * @param int $elementId
  218. * @param int $containerId
  219. * @return string
  220. */
  221. public function setValidationContainer($elementId, $containerId)
  222. {
  223. return '<script type="text/javascript">
  224. $(\'' . $elementId . '\').advaiceContainer = \'' . $containerId . '\';
  225. $(\'' . $elementId . '\').callbackFunction = \'bundle.validationCallback\';
  226. </script>';
  227. }
  228. /**
  229. * Format price string
  230. *
  231. * @param float $price
  232. * @param bool $includeContainer
  233. * @return string
  234. */
  235. public function formatPriceString($price, $includeContainer = true)
  236. {
  237. $taxHelper = Mage::helper('tax');
  238. $coreHelper = $this->helper('core');
  239. $currentProduct = $this->getProduct();
  240. if ($currentProduct->getPriceType() == Mage_Bundle_Model_Product_Price::PRICE_TYPE_DYNAMIC
  241. && $this->getFormatProduct()
  242. ) {
  243. $product = $this->getFormatProduct();
  244. } else {
  245. $product = $currentProduct;
  246. }
  247. $priceTax = $taxHelper->getPrice($product, $price);
  248. $priceIncTax = $taxHelper->getPrice($product, $price, true);
  249. $formated = $coreHelper->currencyByStore($priceTax, $product->getStore(), true, $includeContainer);
  250. if ($taxHelper->displayBothPrices() && $priceTax != $priceIncTax) {
  251. $formated .=
  252. ' (+' .
  253. $coreHelper->currencyByStore($priceIncTax, $product->getStore(), true, $includeContainer) .
  254. ' ' . $this->__('Incl. Tax') . ')';
  255. }
  256. return $formated;
  257. }
  258. }