PageRenderTime 22ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/cinnamong/mag3nt0
PHP | 273 lines | 163 code | 29 blank | 81 comment | 15 complexity | 41d467169633522779eae64b7efd00b4 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. $this->getProduct()->setConfigurablePrice($this->_preparePrice($value['pricing_value'], $value['is_percent']));
  109. Mage::dispatchEvent(
  110. 'catalog_product_type_configurable_price',
  111. array('product' => $this->getProduct())
  112. );
  113. $configurablePrice = $this->getProduct()->getConfigurablePrice();
  114. $info['options'][] = array(
  115. 'id' => $value['value_index'],
  116. 'label' => $value['label'],
  117. 'price' => $configurablePrice,
  118. 'oldPrice' => $this->_preparePrice($value['pricing_value'], $value['is_percent']),
  119. 'products' => isset($options[$attributeId][$value['value_index']]) ? $options[$attributeId][$value['value_index']] : array(),
  120. );
  121. $optionPrices[] = $configurablePrice;
  122. //$this->_registerAdditionalJsPrice($value['pricing_value'], $value['is_percent']);
  123. }
  124. }
  125. /**
  126. * Prepare formated values for options choose
  127. */
  128. foreach ($optionPrices as $optionPrice) {
  129. foreach ($optionPrices as $additional) {
  130. $this->_preparePrice(abs($additional-$optionPrice));
  131. }
  132. }
  133. if($this->_validateAttributeInfo($info)) {
  134. $attributes[$attributeId] = $info;
  135. }
  136. }
  137. /*echo '<pre>';
  138. print_r($this->_prices);
  139. echo '</pre>';die();*/
  140. $_request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false);
  141. $_request->setProductClassId($this->getProduct()->getTaxClassId());
  142. $defaultTax = Mage::getSingleton('tax/calculation')->getRate($_request);
  143. $_request = Mage::getSingleton('tax/calculation')->getRateRequest();
  144. $_request->setProductClassId($this->getProduct()->getTaxClassId());
  145. $currentTax = Mage::getSingleton('tax/calculation')->getRate($_request);
  146. $taxConfig = array(
  147. 'includeTax' => Mage::helper('tax')->priceIncludesTax(),
  148. 'showIncludeTax' => Mage::helper('tax')->displayPriceIncludingTax(),
  149. 'showBothPrices' => Mage::helper('tax')->displayBothPrices(),
  150. 'defaultTax' => $defaultTax,
  151. 'currentTax' => $currentTax,
  152. 'inclTaxTitle' => Mage::helper('catalog')->__('Incl. Tax'),
  153. );
  154. $config = array(
  155. 'attributes' => $attributes,
  156. 'template' => str_replace('%s', '#{price}', $store->getCurrentCurrency()->getOutputFormat()),
  157. // 'prices' => $this->_prices,
  158. 'basePrice' => $this->_registerJsPrice($this->_convertPrice($this->getProduct()->getFinalPrice())),
  159. 'oldPrice' => $this->_registerJsPrice($this->_convertPrice($this->getProduct()->getPrice())),
  160. 'productId' => $this->getProduct()->getId(),
  161. 'chooseText' => Mage::helper('catalog')->__('Choose an Option...'),
  162. 'taxConfig' => $taxConfig,
  163. );
  164. return Mage::helper('core')->jsonEncode($config);
  165. }
  166. /**
  167. * Validating of super product option value
  168. *
  169. * @param array $attribute
  170. * @param array $value
  171. * @param array $options
  172. * @return boolean
  173. */
  174. protected function _validateAttributeValue($attributeId, &$value, &$options)
  175. {
  176. if(isset($options[$attributeId][$value['value_index']])) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. /**
  182. * Validation of super product option
  183. *
  184. * @param array $info
  185. * @return boolean
  186. */
  187. protected function _validateAttributeInfo(&$info)
  188. {
  189. if(count($info['options']) > 0) {
  190. return true;
  191. }
  192. return false;
  193. }
  194. protected function _preparePrice($price, $isPercent=false)
  195. {
  196. if ($isPercent && !empty($price)) {
  197. $price = $this->getProduct()->getPrice()*$price/100;
  198. }
  199. return $this->_registerJsPrice($this->_convertPrice($price, true));
  200. }
  201. protected function _registerJsPrice($price)
  202. {
  203. $jsPrice = str_replace(',', '.', $price);
  204. // if (!isset($this->_prices[$jsPrice])) {
  205. // $this->_prices[$jsPrice] = strip_tags(Mage::app()->getStore()->formatPrice($price));
  206. // }
  207. return $jsPrice;
  208. }
  209. protected function _convertPrice($price, $round=false)
  210. {
  211. if (empty($price)) {
  212. return 0;
  213. }
  214. $price = Mage::app()->getStore()->convertPrice($price);
  215. if ($round) {
  216. $price = Mage::app()->getStore()->roundPrice($price);
  217. }
  218. return $price;
  219. }
  220. // protected function _registerAdditionalJsPrice($price, $isPercent=false)
  221. // {
  222. // if (empty($price) && isset($this->_prices[0])) {
  223. // return $this;
  224. // }
  225. //
  226. // $basePrice = $this->getProduct()->getFinalPrice();
  227. // if ($isPercent) {
  228. // $price = $basePrice*$price/100;
  229. // }
  230. // else {
  231. // $price = $price;
  232. // }
  233. //
  234. // $price = $this->_convertPrice($price);
  235. //
  236. // foreach ($this->_resPrices as $prevPrice) {
  237. // $additionalPrice = $prevPrice + $price;
  238. // $this->_resPrices[] = $additionalPrice;
  239. // $jsAdditionalPrice = str_replace(',', '.', $additionalPrice);
  240. // $this->_prices[$jsAdditionalPrice] = strip_tags(Mage::app()->getStore()->formatPrice($additionalPrice));
  241. // }
  242. // return $this;
  243. // }
  244. }