Home > Magento > Adding Minimal Price To Any Product Collection in Magento

Adding Minimal Price To Any Product Collection in Magento

June 4th, 2009

There are many “Bestseller“, “Most Viewed”, and other front-page modules available for Magento. Those that use the Reports module to maintain good performance have the slight drawback that they do not inherit the catalog/product_collection resource model and it’s handy addMinimalPrices() function. This function adds the fields necessary for the getPriceHtml($product, TRUE) function to work, which prints out the “As low as” text for products that have tiered pricing. This is a must-have feature if you use tiered pricing, so here is how you can make getPriceHTML($product, TRUE) work with any collection of products:

$productIds = array_keys($_products->getItems());
$minimalPriceModel = Mage::getResourceModel('catalogindex/price');
$minimalPriceModel->setStoreId(Mage::app()->getStore()->getId());
$minimalPriceModel->setCustomerGroupId(
  Mage::getSingleton('customer/session')->getCustomerGroupId());
$minimalPrices = $minimalPriceModel->getMinimalPrices($productIds);
foreach ($minimalPrices as $row) {
  $item = $_products->getItemById($row['entity_id']);
  if ($item) {
    $item->setData('minimal_price', $row['value']);
    $item->setData('minimal_tax_class_id', $row['tax_class_id']);
  }
}

There you have it!

Magento ,

  1. achituv
    October 26th, 2009 at 04:19 | #1

    Um, Can you please explain where this code snippet should be used exactly?

    I’ve failed to find where ‘minimal_price’ is actually being set.

  2. October 26th, 2009 at 04:50 | #2

    This is for adding minimal price data to any product collection (there is an easier way for product collections that subclass the catalog/product_collection resource model). You can use it in a template or block where you want to display the minimal prices for each product without re-loading each one individually (which is horribly inefficient).

  1. No trackbacks yet.
Comments are closed.