Magento: Quantity corresponding to minimum price tier
When displaying a product summary for a product with tiered pricing, Magento shows the user how much they can save by showing the minimum price (including tax). Especially with the way the string "As low as" is translated to other languages, this can be confusing and you might want to show the quantity of the product the customer needs to buy to get it at the displayed minimum price. Unfortunately, while Magento has the getMinimalPrice method to retrieve the lowest tier price, it doesn't have a convenient function to retrieve the corresponding tier quantity.
Finding quantity corresponding to minimal price tier
I wanted to add the quantity to the pricing display in app/design/frontend/default/template/template/catalog/product/price.phtml. It's quite a horrendous file where it's tough to find stuff, but search for the string "as low as" and you'll find the right place. You would think you could retrieve the pricing tiers by calling the $this->getTierPrices method with the current product (available as $_product) as parameter. This always returns an empty array, in my experience. Instead, we have to take the roundabout way of re-retrieving the product collection and retrieving the pricing tiers for each product in it. Here's how:
<?php
//Assume sensible defaults
$tier_qty = 1;
$tier_prices = array ();
//Retrieve the product collection
$collection = Mage::getModel ('catalog/product')
->getCollection ()
->addAttributeToSelect ('*')
->addAttributeToFilter ('status', 1)
->addAttributeToFilter ('sku', $_product->getSku ())
->load ();
//Store the tier prices
foreach ($collection as $p)
{
$tier_prices = array_merge ($tier_prices, $this->getTierPrices ($p));
}
if (count ($tier_prices))
{
//Find the one that matches the minimal price
$ref = floatval ($_minimalPriceValue);
foreach ($tier_prices as $p)
{
//Floating point comparison -- use epsilon of 1 hundredth of a cent
if (abs ($p ['price'] - $ref) < 0.0001)
{
//Found it, so stop looking
$tier_qty = $p ['price_qty'];
break;
}
}
}
//use $tier_qty as you see fit
?>
Comments
I suggest another way that extends default Magento Tier Price function by allowing admins to define tier prices as a percentage.
That is using a Magento extension called Percentage Tier Price, you can check it out: https://goo.gl/brXSLX
Post a comment