Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. function getPriceRange($productId) {
  2.  
  3. $max = '';
  4. $min = '';
  5.  
  6. $pricesByAttributeValues = array();
  7.  
  8. $product = Mage::getModel('catalog/product')->load($productId);
  9. $attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
  10. $basePrice = $product->getFinalPrice();
  11.  
  12. foreach ($attributes as $attribute) {
  13. $prices = $attribute->getPrices();
  14. foreach ($prices as $price) {
  15. if ($price['is_percent']){ //if the price is specified in percents
  16. $pricesByAttributeValues[$price['value_index']] = (float)$price['pricing_value'] * $basePrice / 100;
  17. }
  18. else { //if the price is absolute value
  19. $pricesByAttributeValues[$price['value_index']] = (float)$price['pricing_value'];
  20. }
  21. }
  22. }
  23.  
  24. $simple = $product->getTypeInstance()->getUsedProducts();
  25.  
  26. foreach ($simple as $sProduct) {
  27. $totalPrice = $basePrice;
  28. foreach ($attributes as $attribute) {
  29. $value = $sProduct->getData($attribute->getProductAttribute()->getAttributeCode());
  30. if (isset($pricesByAttributeValues[$value])){
  31. $totalPrice += $pricesByAttributeValues[$value];
  32. }
  33. }
  34. if(!$max || $totalPrice > $max)
  35. $max = $totalPrice;
  36. if(!$min || $totalPrice < $min)
  37. $min = $totalPrice;
  38. }
  39.  
  40. if ($min == $max) {
  41. return "$".$min;
  42. } else {
  43. return "$"."$min - "."$"."$max";
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement