Guest User

shakir

a guest
Feb 21st, 2016
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. #The methods in there have become a bit convoluted, so it could benefit from a tidy,
  4. #...though the logic is not that simple any more.
  5.  
  6. class OrganicInternet_SimpleConfigurableProducts_Catalog_Model_Product_Type_Configurable_Price
  7. extends Mage_Catalog_Model_Product_Type_Configurable_Price
  8. {
  9. #We don't want to show a separate 'minimal' price for configurable products.
  10. public function getMinimalPrice($product)
  11. {
  12. return $this->getPrice($product);
  13. }
  14.  
  15. public function getMaxPossibleFinalPrice($product) {
  16. #Indexer calculates max_price, so if this value's been loaded, use it
  17. $price = $product->getMaxPrice();
  18. if ($price !== null) {
  19. return $price;
  20. }
  21.  
  22. $childProduct = $this->getChildProductWithHighestPrice($product, "finalPrice");
  23. #If there aren't any salable child products we return the highest price
  24. #of all child products, including any ones not currently salable.
  25.  
  26. if (!$childProduct) {
  27. $childProduct = $this->getChildProductWithHighestPrice($product, "finalPrice", false);
  28. }
  29.  
  30. if ($childProduct) {
  31. return $childProduct->getFinalPrice();
  32. }
  33. return false;
  34. }
  35.  
  36. #If there aren't any salable child products we return the lowest price
  37. #of all child products, including any ones not currently salable.
  38. public function getFinalPrice($qty=null, $product)
  39. {
  40. //Start edit
  41. $selectedAttributes = array();
  42. if ($product->getCustomOption('attributes')) {
  43. $selectedAttributes = unserialize($product->getCustomOption('attributes')->getValue());
  44. }
  45. //End edit
  46. if (sizeof($selectedAttributes)) return $this->getSimpleProductPrice($qty, $product);
  47.  
  48. if (is_null($qty) && !is_null($product->getCalculatedFinalPrice())) {
  49. return $product->getCalculatedFinalPrice();
  50. }
  51.  
  52. $basePrice = $this->getBasePrice($product, $qty);
  53. $finalPrice = $basePrice;
  54. $product->setFinalPrice($finalPrice);
  55. Mage::dispatchEvent('catalog_product_get_final_price', array('product' => $product, 'qty' => $qty));
  56. $finalPrice = $product->getData('final_price');
  57.  
  58. $finalPrice += $this->getTotalConfigurableItemsPrice($product, $finalPrice);
  59. $finalPrice += $this->_applyOptionsPrice($product, $qty, $basePrice) - $basePrice;
  60. $finalPrice = max(0, $finalPrice);
  61.  
  62. $product->setFinalPrice($finalPrice);
  63. return $finalPrice;
  64. }
  65.  
  66. public function getSimpleProductPrice($qty=null, $product)
  67. {
  68. $cfgId = $product->getId();
  69. $product->getTypeInstance(true)
  70. ->setStoreFilter($product->getStore(), $product);
  71. $attributes = $product->getTypeInstance(true)
  72. ->getConfigurableAttributes($product);
  73. $selectedAttributes = array();
  74. if ($product->getCustomOption('attributes')) {
  75. $selectedAttributes = unserialize($product->getCustomOption('attributes')->getValue());
  76. }
  77. $db = Mage::getSingleton('core/resource')->getConnection('core_read');
  78. $dbMeta = Mage::getSingleton('core/resource');
  79. $sql = <<<SQL
  80. SELECT main_table.entity_id FROM {$dbMeta->getTableName('catalog/product')} `main_table` INNER JOIN
  81. {$dbMeta->getTableName('catalog/product_super_link')} `sl` ON sl.parent_id = {$cfgId}
  82. SQL;
  83. foreach($selectedAttributes as $attributeId => $optionId) {
  84. $alias = "a{$attributeId}";
  85. $sql .= ' INNER JOIN ' . $dbMeta->getTableName('catalog/product') . "_int" . " $alias ON $alias.entity_id = main_table.entity_id AND $alias.attribute_id = $attributeId AND $alias.value = $optionId AND $alias.entity_id = sl.product_id";
  86. }
  87. $id = $db->fetchOne($sql);
  88. return Mage::getModel("catalog/product")->load($id)->getFinalPrice($qty);
  89. }
  90.  
  91. public function getPrice($product)
  92. {
  93. #Just return indexed_price, if it's been fetched already
  94. #(which it will have been for collections, but not on product page)
  95. $price = $product->getIndexedPrice();
  96. if ($price !== null) {
  97. return $price;
  98. }
  99.  
  100. $childProduct = $this->getChildProductWithLowestPrice($product, "finalPrice");
  101. #If there aren't any salable child products we return the lowest price
  102. #of all child products, including any ones not currently salable.
  103. if (!$childProduct) {
  104. $childProduct = $this->getChildProductWithLowestPrice($product, "finalPrice", false);
  105. }
  106.  
  107. if ($childProduct) {
  108. return $childProduct->getPrice();
  109. }
  110.  
  111. return false;
  112. }
  113.  
  114. public function getChildProducts($product, $checkSalable=true)
  115. {
  116. static $childrenCache = array();
  117. $cacheKey = $product->getId() . ':' . $checkSalable;
  118.  
  119. if (isset($childrenCache[$cacheKey])) {
  120. return $childrenCache[$cacheKey];
  121. }
  122.  
  123. $childProducts = $product->getTypeInstance(true)->getUsedProductCollection($product);
  124. $childProducts->addAttributeToSelect(array('price', 'special_price', 'status', 'special_from_date', 'special_to_date'));
  125.  
  126. if ($checkSalable) {
  127. $salableChildProducts = array();
  128. foreach($childProducts as $childProduct) {
  129. if($childProduct->isSalable()) {
  130. $salableChildProducts[] = $childProduct;
  131. }
  132. }
  133. $childProducts = $salableChildProducts;
  134. }
  135.  
  136. $childrenCache[$cacheKey] = $childProducts;
  137. return $childProducts;
  138. }
  139.  
  140. /*
  141. public function getLowestChildPrice($product, $priceType, $checkSalable=true)
  142. {
  143. $childProduct = $this->getChildProductWithLowestPrice($product, $priceType, $checkSalable);
  144. if ($childProduct) {
  145. if ($priceType == "finalPrice") {
  146. $childPrice = $childProduct->getFinalPrice();
  147. } else {
  148. $childPrice = $childProduct->getPrice();
  149. }
  150. } else {
  151. $childPrice = false;
  152. }
  153. return $childPrice;
  154. }
  155. */
  156. #Could no doubt add highest/lowest as param to save 2 near-identical functions
  157. public function getChildProductWithHighestPrice($product, $priceType, $checkSalable=true)
  158. {
  159. $childProducts = $this->getChildProducts($product, $checkSalable);
  160. if (count($childProducts) == 0) { #If config product has no children
  161. return false;
  162. }
  163. $maxPrice = 0;
  164. $maxProd = false;
  165. foreach($childProducts as $childProduct) {
  166. if ($priceType == "finalPrice") {
  167. $thisPrice = $childProduct->getFinalPrice();
  168. } else {
  169. $thisPrice = $childProduct->getPrice();
  170. }
  171. if($thisPrice > $maxPrice) {
  172. $maxPrice = $thisPrice;
  173. $maxProd = $childProduct;
  174. }
  175. }
  176. return $maxProd;
  177. }
  178.  
  179. public function getChildProductWithLowestPrice($product, $priceType, $checkSalable=true)
  180. {
  181. $childProducts = $this->getChildProducts($product, $checkSalable);
  182. if (count($childProducts) == 0) { #If config product has no children
  183. return false;
  184. }
  185. $minPrice = PHP_INT_MAX;
  186. $minProd = false;
  187. foreach($childProducts as $childProduct) {
  188. if ($priceType == "finalPrice") {
  189. $thisPrice = $childProduct->getFinalPrice();
  190. } else {
  191. $thisPrice = $childProduct->getPrice();
  192. }
  193. if($thisPrice < $minPrice) {
  194. $minPrice = $thisPrice;
  195. $minProd = $childProduct;
  196. }
  197. }
  198. return $minProd;
  199. }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. //Force tier pricing to be empty for configurable products:
  208. public function getTierPrice($qty=null, $product)
  209. {
  210. return array();
  211. }
  212.  
  213. // Shakir start
  214.  
  215.  
  216. if($_product->getTypeId() == "configurable"):
  217. $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
  218. $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
  219. foreach($simple_collection as $simple_product){
  220. echo $simple_product->getSku() . " - " . $simple_product->getName() . " - " . Mage::helper('core')->currency($simple_product->getPrice()) . "<br>";
  221. }
  222. endif;
  223.  
  224. // Shakir end
  225.  
  226. }
RAW Paste Data Copied