PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/ticean/magento-mirror
PHP | 324 lines | 193 code | 37 blank | 94 comment | 22 complexity | f64354853238ed9d3788150df8bfd900 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. /**
  71. * retrieve current store
  72. *
  73. * @return Mage_Core_Model_Store
  74. */
  75. public function getCurrentStore()
  76. {
  77. return Mage::app()->getStore();
  78. }
  79. /**
  80. * Returns additional values for js config, con be overriden by descedants
  81. *
  82. * @return array
  83. */
  84. protected function _getAdditionalConfig()
  85. {
  86. return array();
  87. }
  88. /**
  89. * Composes configuration for js
  90. *
  91. * @return string
  92. */
  93. public function getJsonConfig()
  94. {
  95. $attributes = array();
  96. $options = array();
  97. $store = $this->getCurrentStore();
  98. $taxHelper = Mage::helper('tax');
  99. $currentProduct = $this->getProduct();
  100. $preconfiguredFlag = $currentProduct->hasPreconfiguredValues();
  101. if ($preconfiguredFlag) {
  102. $preconfiguredValues = $currentProduct->getPreconfiguredValues();
  103. $defaultValues = array();
  104. }
  105. foreach ($this->getAllowProducts() as $product) {
  106. $productId = $product->getId();
  107. foreach ($this->getAllowAttributes() as $attribute) {
  108. $productAttribute = $attribute->getProductAttribute();
  109. $productAttributeId = $productAttribute->getId();
  110. $attributeValue = $product->getData($productAttribute->getAttributeCode());
  111. if (!isset($options[$productAttributeId])) {
  112. $options[$productAttributeId] = array();
  113. }
  114. if (!isset($options[$productAttributeId][$attributeValue])) {
  115. $options[$productAttributeId][$attributeValue] = array();
  116. }
  117. $options[$productAttributeId][$attributeValue][] = $productId;
  118. }
  119. }
  120. $this->_resPrices = array(
  121. $this->_preparePrice($currentProduct->getFinalPrice())
  122. );
  123. foreach ($this->getAllowAttributes() as $attribute) {
  124. $productAttribute = $attribute->getProductAttribute();
  125. $attributeId = $productAttribute->getId();
  126. $info = array(
  127. 'id' => $productAttribute->getId(),
  128. 'code' => $productAttribute->getAttributeCode(),
  129. 'label' => $attribute->getLabel(),
  130. 'options' => array()
  131. );
  132. $optionPrices = array();
  133. $prices = $attribute->getPrices();
  134. if (is_array($prices)) {
  135. foreach ($prices as $value) {
  136. if(!$this->_validateAttributeValue($attributeId, $value, $options)) {
  137. continue;
  138. }
  139. $currentProduct->setConfigurablePrice($this->_preparePrice($value['pricing_value'], $value['is_percent']));
  140. Mage::dispatchEvent(
  141. 'catalog_product_type_configurable_price',
  142. array('product' => $currentProduct)
  143. );
  144. $configurablePrice = $currentProduct->getConfigurablePrice();
  145. $info['options'][] = array(
  146. 'id' => $value['value_index'],
  147. 'label' => $value['label'],
  148. 'price' => $configurablePrice,
  149. 'oldPrice' => $this->_preparePrice($value['pricing_value'], $value['is_percent']),
  150. 'products' => isset($options[$attributeId][$value['value_index']]) ? $options[$attributeId][$value['value_index']] : array(),
  151. );
  152. $optionPrices[] = $configurablePrice;
  153. //$this->_registerAdditionalJsPrice($value['pricing_value'], $value['is_percent']);
  154. }
  155. }
  156. /**
  157. * Prepare formated values for options choose
  158. */
  159. foreach ($optionPrices as $optionPrice) {
  160. foreach ($optionPrices as $additional) {
  161. $this->_preparePrice(abs($additional-$optionPrice));
  162. }
  163. }
  164. if($this->_validateAttributeInfo($info)) {
  165. $attributes[$attributeId] = $info;
  166. }
  167. // Add attribute default value (if set)
  168. if ($preconfiguredFlag) {
  169. $configValue = $preconfiguredValues->getData('super_attribute/' . $attributeId);
  170. if ($configValue) {
  171. $defaultValues[$attributeId] = $configValue;
  172. }
  173. }
  174. }
  175. $taxCalculation = Mage::getSingleton('tax/calculation');
  176. if (!$taxCalculation->getCustomer() && Mage::registry('current_customer')) {
  177. $taxCalculation->setCustomer(Mage::registry('current_customer'));
  178. }
  179. $_request = $taxCalculation->getRateRequest(false, false, false);
  180. $_request->setProductClassId($currentProduct->getTaxClassId());
  181. $defaultTax = $taxCalculation->getRate($_request);
  182. $_request = $taxCalculation->getRateRequest();
  183. $_request->setProductClassId($currentProduct->getTaxClassId());
  184. $currentTax = $taxCalculation->getRate($_request);
  185. $taxConfig = array(
  186. 'includeTax' => $taxHelper->priceIncludesTax(),
  187. 'showIncludeTax' => $taxHelper->displayPriceIncludingTax(),
  188. 'showBothPrices' => $taxHelper->displayBothPrices(),
  189. 'defaultTax' => $defaultTax,
  190. 'currentTax' => $currentTax,
  191. 'inclTaxTitle' => Mage::helper('catalog')->__('Incl. Tax')
  192. );
  193. $config = array(
  194. 'attributes' => $attributes,
  195. 'template' => str_replace('%s', '#{price}', $store->getCurrentCurrency()->getOutputFormat()),
  196. // 'prices' => $this->_prices,
  197. 'basePrice' => $this->_registerJsPrice($this->_convertPrice($currentProduct->getFinalPrice())),
  198. 'oldPrice' => $this->_registerJsPrice($this->_convertPrice($currentProduct->getPrice())),
  199. 'productId' => $currentProduct->getId(),
  200. 'chooseText' => Mage::helper('catalog')->__('Choose an Option...'),
  201. 'taxConfig' => $taxConfig
  202. );
  203. if ($preconfiguredFlag && !empty($defaultValues)) {
  204. $config['defaultValues'] = $defaultValues;
  205. }
  206. $config = array_merge($config, $this->_getAdditionalConfig());
  207. return Mage::helper('core')->jsonEncode($config);
  208. }
  209. /**
  210. * Validating of super product option value
  211. *
  212. * @param array $attribute
  213. * @param array $value
  214. * @param array $options
  215. * @return boolean
  216. */
  217. protected function _validateAttributeValue($attributeId, &$value, &$options)
  218. {
  219. if(isset($options[$attributeId][$value['value_index']])) {
  220. return true;
  221. }
  222. return false;
  223. }
  224. /**
  225. * Validation of super product option
  226. *
  227. * @param array $info
  228. * @return boolean
  229. */
  230. protected function _validateAttributeInfo(&$info)
  231. {
  232. if(count($info['options']) > 0) {
  233. return true;
  234. }
  235. return false;
  236. }
  237. protected function _preparePrice($price, $isPercent=false)
  238. {
  239. if ($isPercent && !empty($price)) {
  240. $price = $this->getProduct()->getPrice()*$price/100;
  241. }
  242. return $this->_registerJsPrice($this->_convertPrice($price, true));
  243. }
  244. protected function _registerJsPrice($price)
  245. {
  246. $jsPrice = str_replace(',', '.', $price);
  247. // if (!isset($this->_prices[$jsPrice])) {
  248. // $this->_prices[$jsPrice] = strip_tags(Mage::app()->getStore()->formatPrice($price));
  249. // }
  250. return $jsPrice;
  251. }
  252. protected function _convertPrice($price, $round=false)
  253. {
  254. if (empty($price)) {
  255. return 0;
  256. }
  257. $price = $this->getCurrentStore()->convertPrice($price);
  258. if ($round) {
  259. $price = $this->getCurrentStore()->roundPrice($price);
  260. }
  261. return $price;
  262. }
  263. // protected function _registerAdditionalJsPrice($price, $isPercent=false)
  264. // {
  265. // if (empty($price) && isset($this->_prices[0])) {
  266. // return $this;
  267. // }
  268. //
  269. // $basePrice = $this->getProduct()->getFinalPrice();
  270. // if ($isPercent) {
  271. // $price = $basePrice*$price/100;
  272. // }
  273. // else {
  274. // $price = $price;
  275. // }
  276. //
  277. // $price = $this->_convertPrice($price);
  278. //
  279. // foreach ($this->_resPrices as $prevPrice) {
  280. // $additionalPrice = $prevPrice + $price;
  281. // $this->_resPrices[] = $additionalPrice;
  282. // $jsAdditionalPrice = str_replace(',', '.', $additionalPrice);
  283. // $this->_prices[$jsAdditionalPrice] = strip_tags(Mage::app()->getStore()->formatPrice($additionalPrice));
  284. // }
  285. // return $this;
  286. // }
  287. }