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 class Mage_Catalog_Model_Product_Type_Price
00035 {
00036 const CACHE_TAG = 'PRODUCT_PRICE';
00037
00038 static $attributeCache = array();
00039
00040
00041
00042
00043
00044
00045 public function getPrice($product)
00046 {
00047 return $product->getData('price');
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057 public function getFinalPrice($qty=null, $product)
00058 {
00059 if (is_null($qty) && !is_null($product->getCalculatedFinalPrice())) {
00060 return $product->getCalculatedFinalPrice();
00061 }
00062
00063 $finalPrice = $product->getPrice();
00064 $finalPrice = $this->_applyTierPrice($product, $qty, $finalPrice);
00065 $finalPrice = $this->_applySpecialPrice($product, $finalPrice);
00066 $product->setFinalPrice($finalPrice);
00067
00068 Mage::dispatchEvent('catalog_product_get_final_price', array('product'=>$product));
00069
00070 $finalPrice = $product->getData('final_price');
00071 $finalPrice = $this->_applyOptionsPrice($product, $qty, $finalPrice);
00072
00073 return max(0, $finalPrice);
00074 }
00075
00076 public function getChildFinalPrice($product, $productQty, $childProduct, $childProductQty)
00077 {
00078 return $this->getFinalPrice($childProductQty, $childProduct);
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 protected function _applyTierPrice($product, $qty, $finalPrice)
00090 {
00091 if (is_null($qty)) {
00092 return $finalPrice;
00093 }
00094
00095 $tierPrice = $product->getTierPrice($qty);
00096 if (is_numeric($tierPrice)) {
00097 $finalPrice = min($finalPrice, $tierPrice);
00098 }
00099 return $finalPrice;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108
00109 public function getTierPrice($qty=null, $product)
00110 {
00111 $allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL;
00112 $prices = $product->getData('tier_price');
00113
00114 if (is_null($prices)) {
00115 if ($attribute = $product->getResource()->getAttribute('tier_price')) {
00116 $attribute->getBackend()->afterLoad($product);
00117 $prices = $product->getData('tier_price');
00118 }
00119 }
00120
00121 if (is_null($prices) || !is_array($prices)) {
00122 if (!is_null($qty)) {
00123 return $product->getPrice();
00124 }
00125 return array(array(
00126 'price' => $product->getPrice(),
00127 'website_price' => $product->getPrice(),
00128 'price_qty' => 1,
00129 'cust_group' => $allGroups,
00130 ));
00131 }
00132
00133 $custGroup = $this->_getCustomerGroupId($product);
00134 if ($qty) {
00135 $prevQty = 1;
00136 $prevPrice = $product->getPrice();
00137 $prevGroup = $allGroups;
00138
00139 foreach ($prices as $price) {
00140 if ($price['cust_group']!=$custGroup && $price['cust_group']!=$allGroups) {
00141
00142 continue;
00143 }
00144 if ($qty < $price['price_qty']) {
00145
00146 continue;
00147 }
00148 if ($price['price_qty'] < $prevQty) {
00149
00150 continue;
00151 }
00152 if ($price['price_qty'] == $prevQty && $prevGroup != $allGroups && $price['cust_group'] == $allGroups) {
00153
00154 continue;
00155 }
00156 $prevPrice = $price['website_price'];
00157 $prevQty = $price['price_qty'];
00158 $prevGroup = $price['cust_group'];
00159 }
00160 return $prevPrice;
00161 } else {
00162 foreach ($prices as $i=>$price) {
00163 if ($price['cust_group']!=$custGroup && $price['cust_group']!=$allGroups) {
00164 unset($prices[$i]);
00165 }
00166 }
00167 }
00168
00169 return ($prices) ? $prices : array();
00170 }
00171
00172 protected function _getCustomerGroupId($product)
00173 {
00174 if ($product->getCustomerGroupId()) {
00175 return $product->getCustomerGroupId();
00176 }
00177 return Mage::getSingleton('customer/session')->getCustomerGroupId();
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187 protected function _applySpecialPrice($product, $finalPrice)
00188 {
00189 return $this->calculateSpecialPrice($finalPrice, $product->getSpecialPrice(), $product->getSpecialFromDate(), $product->getSpecialToDate(), $product->getStore());
00190 }
00191
00192
00193
00194
00195
00196
00197
00198 public function getTierPriceCount($product)
00199 {
00200 $price = $product->getTierPrice();
00201 return count($price);
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211 public function getFormatedTierPrice($qty=null, $product)
00212 {
00213 $price = $product->getTierPrice($qty);
00214 if (is_array($price)) {
00215 foreach ($price as $index => $value) {
00216 $price[$index]['formated_price'] = Mage::app()->getStore()->convertPrice($price[$index]['website_price'], true);
00217 }
00218 }
00219 else {
00220 $price = Mage::app()->getStore()->formatPrice($price);
00221 }
00222
00223 return $price;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232 public function getFormatedPrice($product)
00233 {
00234 return Mage::app()->getStore()->formatPrice($product->getFinalPrice());
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 protected function _applyOptionsPrice($product, $qty, $finalPrice)
00246 {
00247 if ($optionIds = $product->getCustomOption('option_ids')) {
00248 $basePrice = $finalPrice;
00249 foreach (explode(',', $optionIds->getValue()) as $optionId) {
00250 if ($option = $product->getOptionById($optionId)) {
00251
00252 $quoteItemOption = $product->getCustomOption('option_'.$option->getId());
00253 $group = $option->groupFactory($option->getType())
00254 ->setOption($option)
00255 ->setQuoteItemOption($quoteItemOption);
00256
00257 $finalPrice += $group->getOptionPrice($quoteItemOption->getValue(), $basePrice);
00258 }
00259 }
00260 }
00261
00262 return $finalPrice;
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 public static function calculatePrice($basePrice, $specialPrice, $specialPriceFrom, $specialPriceTo, $rulePrice = false, $wId = null, $gId = null, $productId = null)
00279 {
00280 Varien_Profiler::start('__PRODUCT_CALCULATE_PRICE__');
00281 if ($wId instanceof Mage_Core_Model_Store) {
00282 $sId = $wId->getId();
00283 $wId = $wId->getWebsiteId();
00284 } else {
00285 $sId = Mage::app()->getWebsite($wId)->getDefaultGroup()->getDefaultStoreId();
00286 }
00287
00288 $finalPrice = $basePrice;
00289 if ($gId instanceof Mage_Customer_Model_Group) {
00290 $gId = $gId->getId();
00291 }
00292
00293 $finalPrice = self::calculateSpecialPrice($finalPrice, $specialPrice, $specialPriceFrom, $specialPriceTo, $sId);
00294
00295 if ($rulePrice === false) {
00296 $storeTimestamp = Mage::app()->getLocale()->storeTimeStamp($sId);
00297 $rulePrice = Mage::getResourceModel('catalogrule/rule')
00298 ->getRulePrice($storeTimestamp, $wId, $gId, $productId);
00299 }
00300
00301 if ($rulePrice !== null && $rulePrice !== false) {
00302 $finalPrice = min($finalPrice, $rulePrice);
00303 }
00304
00305 $finalPrice = max($finalPrice, 0);
00306 Varien_Profiler::stop('__PRODUCT_CALCULATE_PRICE__');
00307 return $finalPrice;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 public static function calculateSpecialPrice($finalPrice, $specialPrice, $specialPriceFrom, $specialPriceTo, $store = null)
00321 {
00322 if (!is_null($specialPrice) && $specialPrice != false) {
00323 if (Mage::app()->getLocale()->IsStoreDateInInterval($store, $specialPriceFrom, $specialPriceTo)) {
00324 $finalPrice = min($finalPrice, $specialPrice);
00325 }
00326 }
00327 return $finalPrice;
00328 }
00329 }