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

/xampp/htdocs/magento/app/code/core/Mage/Catalog/Model/Product/Type/Price.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 329 lines | 176 code | 34 blank | 119 comment | 38 complexity | a9b9217ae0bed8b683ae5ecbd6a926cd 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) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Product type price model
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Type_Price
  34. {
  35. const CACHE_TAG = 'PRODUCT_PRICE';
  36. static $attributeCache = array();
  37. /**
  38. * Default action to get price of product
  39. *
  40. * @return decimal
  41. */
  42. public function getPrice($product)
  43. {
  44. return $product->getData('price');
  45. }
  46. /**
  47. * Get product final price
  48. *
  49. * @param double $qty
  50. * @param Mage_Catalog_Model_Product $product
  51. * @return double
  52. */
  53. public function getFinalPrice($qty=null, $product)
  54. {
  55. if (is_null($qty) && !is_null($product->getCalculatedFinalPrice())) {
  56. return $product->getCalculatedFinalPrice();
  57. }
  58. $finalPrice = $product->getPrice();
  59. $finalPrice = $this->_applyTierPrice($product, $qty, $finalPrice);
  60. $finalPrice = $this->_applySpecialPrice($product, $finalPrice);
  61. $product->setFinalPrice($finalPrice);
  62. Mage::dispatchEvent('catalog_product_get_final_price', array('product'=>$product));
  63. $finalPrice = $product->getData('final_price');
  64. $finalPrice = $this->_applyOptionsPrice($product, $qty, $finalPrice);
  65. return max(0, $finalPrice);
  66. }
  67. public function getChildFinalPrice($product, $productQty, $childProduct, $childProductQty)
  68. {
  69. return $this->getFinalPrice($childProductQty, $childProduct);
  70. }
  71. /**
  72. * Apply tier price for product if not return price that was before
  73. *
  74. * @param Mage_Catalog_Model_Product $product
  75. * @param double $qty
  76. * @param double $finalPrice
  77. * @return double
  78. */
  79. protected function _applyTierPrice($product, $qty, $finalPrice)
  80. {
  81. if (is_null($qty)) {
  82. return $finalPrice;
  83. }
  84. $tierPrice = $product->getTierPrice($qty);
  85. if (is_numeric($tierPrice)) {
  86. $finalPrice = min($finalPrice, $tierPrice);
  87. }
  88. return $finalPrice;
  89. }
  90. /**
  91. * Get product tier price by qty
  92. *
  93. * @param double $qty
  94. * @param Mage_Catalog_Model_Product $product
  95. * @return double
  96. */
  97. public function getTierPrice($qty=null, $product)
  98. {
  99. $allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL;
  100. $prices = $product->getData('tier_price');
  101. if (is_null($prices)) {
  102. if ($attribute = $product->getResource()->getAttribute('tier_price')) {
  103. $attribute->getBackend()->afterLoad($product);
  104. $prices = $product->getData('tier_price');
  105. }
  106. }
  107. if (is_null($prices) || !is_array($prices)) {
  108. if (!is_null($qty)) {
  109. return $product->getPrice();
  110. }
  111. return array(array(
  112. 'price' => $product->getPrice(),
  113. 'website_price' => $product->getPrice(),
  114. 'price_qty' => 1,
  115. 'cust_group' => $allGroups,
  116. ));
  117. }
  118. $custGroup = $this->_getCustomerGroupId($product);
  119. if ($qty) {
  120. $prevQty = 1;
  121. $prevPrice = $product->getPrice();
  122. $prevGroup = $allGroups;
  123. foreach ($prices as $price) {
  124. if ($price['cust_group']!=$custGroup && $price['cust_group']!=$allGroups) {
  125. // tier not for current customer group nor is for all groups
  126. continue;
  127. }
  128. if ($qty < $price['price_qty']) {
  129. // tier is higher than product qty
  130. continue;
  131. }
  132. if ($price['price_qty'] < $prevQty) {
  133. // higher tier qty already found
  134. continue;
  135. }
  136. if ($price['price_qty'] == $prevQty && $prevGroup != $allGroups && $price['cust_group'] == $allGroups) {
  137. // found tier qty is same as current tier qty but current tier group is ALL_GROUPS
  138. continue;
  139. }
  140. $prevPrice = $price['website_price'];
  141. $prevQty = $price['price_qty'];
  142. $prevGroup = $price['cust_group'];
  143. }
  144. return $prevPrice;
  145. } else {
  146. foreach ($prices as $i=>$price) {
  147. if ($price['cust_group']!=$custGroup && $price['cust_group']!=$allGroups) {
  148. unset($prices[$i]);
  149. }
  150. }
  151. }
  152. return ($prices) ? $prices : array();
  153. }
  154. protected function _getCustomerGroupId($product)
  155. {
  156. if ($product->getCustomerGroupId()) {
  157. return $product->getCustomerGroupId();
  158. }
  159. return Mage::getSingleton('customer/session')->getCustomerGroupId();
  160. }
  161. /**
  162. * Apply special price for product if not return price that was before
  163. *
  164. * @param Mage_Catalog_Model_Product $product
  165. * @param double $finalPrice
  166. * @return double
  167. */
  168. protected function _applySpecialPrice($product, $finalPrice)
  169. {
  170. return $this->calculateSpecialPrice($finalPrice, $product->getSpecialPrice(), $product->getSpecialFromDate(), $product->getSpecialToDate(), $product->getStore());
  171. }
  172. /**
  173. * Count how many tier prices we have for the product
  174. *
  175. * @param Mage_Catalog_Model_Product $product
  176. * @return int
  177. */
  178. public function getTierPriceCount($product)
  179. {
  180. $price = $product->getTierPrice();
  181. return count($price);
  182. }
  183. /**
  184. * Get formated by currency tier price
  185. *
  186. * @param double $qty
  187. * @param Mage_Catalog_Model_Product $product
  188. * @return array || double
  189. */
  190. public function getFormatedTierPrice($qty=null, $product)
  191. {
  192. $price = $product->getTierPrice($qty);
  193. if (is_array($price)) {
  194. foreach ($price as $index => $value) {
  195. $price[$index]['formated_price'] = Mage::app()->getStore()->convertPrice($price[$index]['website_price'], true);
  196. }
  197. }
  198. else {
  199. $price = Mage::app()->getStore()->formatPrice($price);
  200. }
  201. return $price;
  202. }
  203. /**
  204. * Get formated by currency product price
  205. *
  206. * @param Mage_Catalog_Model_Product $product
  207. * @return array || double
  208. */
  209. public function getFormatedPrice($product)
  210. {
  211. return Mage::app()->getStore()->formatPrice($product->getFinalPrice());
  212. }
  213. /**
  214. * Apply options price
  215. *
  216. * @param Mage_Catalog_Model_Product $product
  217. * @param int $qty
  218. * @param double $finalPrice
  219. * @return double
  220. */
  221. protected function _applyOptionsPrice($product, $qty, $finalPrice)
  222. {
  223. if ($optionIds = $product->getCustomOption('option_ids')) {
  224. $basePrice = $finalPrice;
  225. foreach (explode(',', $optionIds->getValue()) as $optionId) {
  226. if ($option = $product->getOptionById($optionId)) {
  227. $quoteItemOption = $product->getCustomOption('option_'.$option->getId());
  228. $group = $option->groupFactory($option->getType())
  229. ->setOption($option)
  230. ->setQuoteItemOption($quoteItemOption);
  231. $finalPrice += $group->getOptionPrice($quoteItemOption->getValue(), $basePrice);
  232. }
  233. }
  234. }
  235. return $finalPrice;
  236. }
  237. /**
  238. * Calculate product price based on special price data and price rules
  239. *
  240. * @param float $basePrice
  241. * @param float $specialPrice
  242. * @param string $specialPriceFrom
  243. * @param string $specialPriceTo
  244. * @param float|null|false $rulePrice
  245. * @param mixed $wId
  246. * @param mixed $gId
  247. * @param null|int $productId
  248. * @return float
  249. */
  250. public static function calculatePrice($basePrice, $specialPrice, $specialPriceFrom, $specialPriceTo, $rulePrice = false, $wId = null, $gId = null, $productId = null)
  251. {
  252. Varien_Profiler::start('__PRODUCT_CALCULATE_PRICE__');
  253. if ($wId instanceof Mage_Core_Model_Store) {
  254. $sId = $wId->getId();
  255. $wId = $wId->getWebsiteId();
  256. } else {
  257. $sId = Mage::app()->getWebsite($wId)->getDefaultGroup()->getDefaultStoreId();
  258. }
  259. $finalPrice = $basePrice;
  260. if ($gId instanceof Mage_Customer_Model_Group) {
  261. $gId = $gId->getId();
  262. }
  263. $finalPrice = self::calculateSpecialPrice($finalPrice, $specialPrice, $specialPriceFrom, $specialPriceTo, $sId);
  264. if ($rulePrice === false) {
  265. $storeTimestamp = Mage::app()->getLocale()->storeTimeStamp($sId);
  266. $rulePrice = Mage::getResourceModel('catalogrule/rule')
  267. ->getRulePrice($storeTimestamp, $wId, $gId, $productId);
  268. }
  269. if ($rulePrice !== null && $rulePrice !== false) {
  270. $finalPrice = min($finalPrice, $rulePrice);
  271. }
  272. $finalPrice = max($finalPrice, 0);
  273. Varien_Profiler::stop('__PRODUCT_CALCULATE_PRICE__');
  274. return $finalPrice;
  275. }
  276. /**
  277. * Calculate and apply special price
  278. *
  279. * @param float $finalPrice
  280. * @param float $specialPrice
  281. * @param string $specialPriceFrom
  282. * @param string $specialPriceTo
  283. * @param mixed $store
  284. * @return float
  285. */
  286. public static function calculateSpecialPrice($finalPrice, $specialPrice, $specialPriceFrom, $specialPriceTo, $store = null)
  287. {
  288. if (!is_null($specialPrice) && $specialPrice != false) {
  289. if (Mage::app()->getLocale()->IsStoreDateInInterval($store, $specialPriceFrom, $specialPriceTo)) {
  290. $finalPrice = min($finalPrice, $specialPrice);
  291. }
  292. }
  293. return $finalPrice;
  294. }
  295. }