PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/gryzz/crystal_magento
PHP | 266 lines | 156 code | 29 blank | 81 comment | 15 complexity | 99e58d1ee922f47046276df00ccdb550 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_Catalog
  23. * @copyright Copyright (c) 2010 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. * Catalog super product configurable part block
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Block_Product_View_Type_Configurable extends Mage_Catalog_Block_Product_View_Abstract
  34. {
  35. protected $_prices = array();
  36. protected $_resPrices = array();
  37. public function getAllowAttributes()
  38. {
  39. return $this->getProduct()->getTypeInstance(true)
  40. ->getConfigurableAttributes($this->getProduct());
  41. }
  42. public function hasOptions()
  43. {
  44. $attributes = $this->getAllowAttributes();
  45. if (count($attributes)) {
  46. foreach ($attributes as $key => $attribute) {
  47. /** @var Mage_Catalog_Model_Product_Type_Configurable_Attribute $attribute */
  48. if ($attribute->getData('prices')) {
  49. return true;
  50. }
  51. }
  52. }
  53. return false;
  54. }
  55. public function getAllowProducts()
  56. {
  57. if (!$this->hasAllowProducts()) {
  58. $products = array();
  59. $allProducts = $this->getProduct()->getTypeInstance(true)
  60. ->getUsedProducts(null, $this->getProduct());
  61. foreach ($allProducts as $product) {
  62. if ($product->isSaleable()) {
  63. $products[] = $product;
  64. }
  65. }
  66. $this->setAllowProducts($products);
  67. }
  68. return $this->getData('allow_products');
  69. }
  70. public function getJsonConfig()
  71. {
  72. $attributes = array();
  73. $options = array();
  74. $store = Mage::app()->getStore();
  75. foreach ($this->getAllowProducts() as $product) {
  76. $productId = $product->getId();
  77. foreach ($this->getAllowAttributes() as $attribute) {
  78. $productAttribute = $attribute->getProductAttribute();
  79. $attributeValue = $product->getData($productAttribute->getAttributeCode());
  80. if (!isset($options[$productAttribute->getId()])) {
  81. $options[$productAttribute->getId()] = array();
  82. }
  83. if (!isset($options[$productAttribute->getId()][$attributeValue])) {
  84. $options[$productAttribute->getId()][$attributeValue] = array();
  85. }
  86. $options[$productAttribute->getId()][$attributeValue][] = $productId;
  87. }
  88. }
  89. $this->_resPrices = array(
  90. $this->_preparePrice($this->getProduct()->getFinalPrice())
  91. );
  92. foreach ($this->getAllowAttributes() as $attribute) {
  93. $productAttribute = $attribute->getProductAttribute();
  94. $attributeId = $productAttribute->getId();
  95. $info = array(
  96. 'id' => $productAttribute->getId(),
  97. 'code' => $productAttribute->getAttributeCode(),
  98. 'label' => $attribute->getLabel(),
  99. 'options' => array()
  100. );
  101. $optionPrices = array();
  102. $prices = $attribute->getPrices();
  103. if (is_array($prices)) {
  104. foreach ($prices as $value) {
  105. if(!$this->_validateAttributeValue($attributeId, $value, $options)) {
  106. continue;
  107. }
  108. $info['options'][] = array(
  109. 'id' => $value['value_index'],
  110. 'label' => $value['label'],
  111. 'price' => $this->_preparePrice($value['pricing_value'], $value['is_percent']),
  112. 'products' => isset($options[$attributeId][$value['value_index']]) ? $options[$attributeId][$value['value_index']] : array(),
  113. );
  114. $optionPrices[] = $this->_preparePrice($value['pricing_value'], $value['is_percent']);
  115. //$this->_registerAdditionalJsPrice($value['pricing_value'], $value['is_percent']);
  116. }
  117. }
  118. /**
  119. * Prepare formated values for options choose
  120. */
  121. foreach ($optionPrices as $optionPrice) {
  122. foreach ($optionPrices as $additional) {
  123. $this->_preparePrice(abs($additional-$optionPrice));
  124. }
  125. }
  126. if($this->_validateAttributeInfo($info)) {
  127. $attributes[$attributeId] = $info;
  128. }
  129. }
  130. /*echo '<pre>';
  131. print_r($this->_prices);
  132. echo '</pre>';die();*/
  133. $_request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false);
  134. $_request->setProductClassId($this->getProduct()->getTaxClassId());
  135. $defaultTax = Mage::getSingleton('tax/calculation')->getRate($_request);
  136. $_request = Mage::getSingleton('tax/calculation')->getRateRequest();
  137. $_request->setProductClassId($this->getProduct()->getTaxClassId());
  138. $currentTax = Mage::getSingleton('tax/calculation')->getRate($_request);
  139. $taxConfig = array(
  140. 'includeTax' => Mage::helper('tax')->priceIncludesTax(),
  141. 'showIncludeTax' => Mage::helper('tax')->displayPriceIncludingTax(),
  142. 'showBothPrices' => Mage::helper('tax')->displayBothPrices(),
  143. 'defaultTax' => $defaultTax,
  144. 'currentTax' => $currentTax,
  145. 'inclTaxTitle' => Mage::helper('catalog')->__('Incl. Tax'),
  146. );
  147. $config = array(
  148. 'attributes' => $attributes,
  149. 'template' => str_replace('%s', '#{price}', $store->getCurrentCurrency()->getOutputFormat()),
  150. // 'prices' => $this->_prices,
  151. 'basePrice' => $this->_registerJsPrice($this->_convertPrice($this->getProduct()->getFinalPrice())),
  152. 'oldPrice' => $this->_registerJsPrice($this->_convertPrice($this->getProduct()->getPrice())),
  153. 'productId' => $this->getProduct()->getId(),
  154. 'chooseText' => Mage::helper('catalog')->__('Choose an Option...'),
  155. 'taxConfig' => $taxConfig,
  156. );
  157. return Mage::helper('core')->jsonEncode($config);
  158. }
  159. /**
  160. * Validating of super product option value
  161. *
  162. * @param array $attribute
  163. * @param array $value
  164. * @param array $options
  165. * @return boolean
  166. */
  167. protected function _validateAttributeValue($attributeId, &$value, &$options)
  168. {
  169. if(isset($options[$attributeId][$value['value_index']])) {
  170. return true;
  171. }
  172. return false;
  173. }
  174. /**
  175. * Validation of super product option
  176. *
  177. * @param array $info
  178. * @return boolean
  179. */
  180. protected function _validateAttributeInfo(&$info)
  181. {
  182. if(count($info['options']) > 0) {
  183. return true;
  184. }
  185. return false;
  186. }
  187. protected function _preparePrice($price, $isPercent=false)
  188. {
  189. if ($isPercent && !empty($price)) {
  190. $price = $this->getProduct()->getFinalPrice()*$price/100;
  191. }
  192. return $this->_registerJsPrice($this->_convertPrice($price, true));
  193. }
  194. protected function _registerJsPrice($price)
  195. {
  196. $jsPrice = str_replace(',', '.', $price);
  197. // if (!isset($this->_prices[$jsPrice])) {
  198. // $this->_prices[$jsPrice] = strip_tags(Mage::app()->getStore()->formatPrice($price));
  199. // }
  200. return $jsPrice;
  201. }
  202. protected function _convertPrice($price, $round=false)
  203. {
  204. if (empty($price)) {
  205. return 0;
  206. }
  207. $price = Mage::app()->getStore()->convertPrice($price);
  208. if ($round) {
  209. $price = Mage::app()->getStore()->roundPrice($price);
  210. }
  211. return $price;
  212. }
  213. // protected function _registerAdditionalJsPrice($price, $isPercent=false)
  214. // {
  215. // if (empty($price) && isset($this->_prices[0])) {
  216. // return $this;
  217. // }
  218. //
  219. // $basePrice = $this->getProduct()->getFinalPrice();
  220. // if ($isPercent) {
  221. // $price = $basePrice*$price/100;
  222. // }
  223. // else {
  224. // $price = $price;
  225. // }
  226. //
  227. // $price = $this->_convertPrice($price);
  228. //
  229. // foreach ($this->_resPrices as $prevPrice) {
  230. // $additionalPrice = $prevPrice + $price;
  231. // $this->_resPrices[] = $additionalPrice;
  232. // $jsAdditionalPrice = str_replace(',', '.', $additionalPrice);
  233. // $this->_prices[$jsAdditionalPrice] = strip_tags(Mage::app()->getStore()->formatPrice($additionalPrice));
  234. // }
  235. // return $this;
  236. // }
  237. }