00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Block_Product_View_Type_Configurable extends Mage_Catalog_Block_Product_View_Abstract
00036 {
00037 protected $_prices = array();
00038 protected $_resPrices = array();
00039
00040 public function getAllowAttributes()
00041 {
00042 return $this->getProduct()->getTypeInstance(true)
00043 ->getConfigurableAttributes($this->getProduct());
00044 }
00045
00046 public function hasOptions()
00047 {
00048 $attributes = $this->getAllowAttributes();
00049 if (count($attributes)) {
00050 foreach ($attributes as $key => $attribute) {
00051
00052 if ($attribute->getData('prices')) {
00053 return true;
00054 }
00055 }
00056 }
00057 return false;
00058 }
00059
00060 public function getAllowProducts()
00061 {
00062 if (!$this->hasAllowProducts()) {
00063 $products = array();
00064 $allProducts = $this->getProduct()->getTypeInstance(true)
00065 ->getUsedProducts(null, $this->getProduct());
00066 foreach ($allProducts as $product) {
00067 if ($product->isSaleable()) {
00068 $products[] = $product;
00069 }
00070 }
00071 $this->setAllowProducts($products);
00072 }
00073 return $this->getData('allow_products');
00074 }
00075
00076 public function getJsonConfig()
00077 {
00078 $attributes = array();
00079 $options = array();
00080 $store = Mage::app()->getStore();
00081 foreach ($this->getAllowProducts() as $product) {
00082 $productId = $product->getId();
00083
00084 foreach ($this->getAllowAttributes() as $attribute) {
00085 $productAttribute = $attribute->getProductAttribute();
00086 $attributeValue = $product->getData($productAttribute->getAttributeCode());
00087 if (!isset($options[$productAttribute->getId()])) {
00088 $options[$productAttribute->getId()] = array();
00089 }
00090
00091 if (!isset($options[$productAttribute->getId()][$attributeValue])) {
00092 $options[$productAttribute->getId()][$attributeValue] = array();
00093 }
00094 $options[$productAttribute->getId()][$attributeValue][] = $productId;
00095 }
00096 }
00097
00098 $this->_resPrices = array(
00099 $this->_preparePrice($this->getProduct()->getFinalPrice())
00100 );
00101
00102 foreach ($this->getAllowAttributes() as $attribute) {
00103 $productAttribute = $attribute->getProductAttribute();
00104 $attributeId = $productAttribute->getId();
00105 $info = array(
00106 'id' => $productAttribute->getId(),
00107 'code' => $productAttribute->getAttributeCode(),
00108 'label' => $attribute->getLabel(),
00109 'options' => array()
00110 );
00111
00112 $optionPrices = array();
00113 $prices = $attribute->getPrices();
00114 if (is_array($prices)) {
00115 foreach ($prices as $value) {
00116 if(!$this->_validateAttributeValue($attributeId, $value, $options)) {
00117 continue;
00118 }
00119
00120 $info['options'][] = array(
00121 'id' => $value['value_index'],
00122 'label' => $value['label'],
00123 'price' => $this->_preparePrice($value['pricing_value'], $value['is_percent']),
00124 'products' => isset($options[$attributeId][$value['value_index']]) ? $options[$attributeId][$value['value_index']] : array(),
00125 );
00126 $optionPrices[] = $this->_preparePrice($value['pricing_value'], $value['is_percent']);
00127
00128 }
00129 }
00130
00131
00132
00133 foreach ($optionPrices as $optionPrice) {
00134 foreach ($optionPrices as $additional) {
00135 $this->_preparePrice(abs($additional-$optionPrice));
00136 }
00137 }
00138 if($this->_validateAttributeInfo($info)) {
00139 $attributes[$attributeId] = $info;
00140 }
00141 }
00142
00143
00144
00145
00146 $_request = Mage::getSingleton('tax/calculation')->getRateRequest(false, false, false);
00147 $_request->setProductClassId($this->getProduct()->getTaxClassId());
00148 $defaultTax = Mage::getSingleton('tax/calculation')->getRate($_request);
00149
00150 $_request = Mage::getSingleton('tax/calculation')->getRateRequest();
00151 $_request->setProductClassId($this->getProduct()->getTaxClassId());
00152 $currentTax = Mage::getSingleton('tax/calculation')->getRate($_request);
00153
00154 $taxConfig = array(
00155 'includeTax' => Mage::helper('tax')->priceIncludesTax(),
00156 'showIncludeTax' => Mage::helper('tax')->displayPriceIncludingTax(),
00157 'showBothPrices' => Mage::helper('tax')->displayBothPrices(),
00158 'defaultTax' => $defaultTax,
00159 'currentTax' => $currentTax,
00160 'inclTaxTitle' => Mage::helper('catalog')->__('Incl. Tax'),
00161 );
00162
00163 $config = array(
00164 'attributes' => $attributes,
00165 'template' => str_replace('%s', '#{price}', $store->getCurrentCurrency()->getOutputFormat()),
00166
00167 'basePrice' => $this->_registerJsPrice($this->_convertPrice($this->getProduct()->getFinalPrice())),
00168 'oldPrice' => $this->_registerJsPrice($this->_convertPrice($this->getProduct()->getPrice())),
00169 'productId' => $this->getProduct()->getId(),
00170 'chooseText' => Mage::helper('catalog')->__('Choose option...'),
00171 'taxConfig' => $taxConfig,
00172 );
00173
00174 return Zend_Json::encode($config);
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185 protected function _validateAttributeValue($attributeId, &$value, &$options)
00186 {
00187 if(isset($options[$attributeId][$value['value_index']])) {
00188 return true;
00189 }
00190
00191 return false;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 protected function _validateAttributeInfo(&$info)
00201 {
00202 if(count($info['options']) > 0) {
00203 return true;
00204 }
00205 return false;
00206 }
00207
00208 protected function _preparePrice($price, $isPercent=false)
00209 {
00210 if ($isPercent && !empty($price)) {
00211 $price = $this->getProduct()->getFinalPrice()*$price/100;
00212 }
00213
00214 return $this->_registerJsPrice($this->_convertPrice($price, true));
00215 }
00216
00217 protected function _registerJsPrice($price)
00218 {
00219 $jsPrice = str_replace(',', '.', $price);
00220
00221
00222
00223
00224 return $jsPrice;
00225 }
00226
00227 protected function _convertPrice($price, $round=false)
00228 {
00229 if (empty($price)) {
00230 return 0;
00231 }
00232
00233 $price = Mage::app()->getStore()->convertPrice($price);
00234 if ($round) {
00235 $price = Mage::app()->getStore()->roundPrice($price);
00236 }
00237
00238
00239 return $price;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 }