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

Adding Minimal Price To Any Product Collection in Magento

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 ,

  • achituv

    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.

  • http://colin.mollenhour.com colin

    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).

  • Muzafar Ibrahim

    hi,
    this code not support the store id how i can fetch product by store..?

  • http://colin.mollenhour.com Colin Mollenhour

    Line 1 starts by assuming you already have a product collection ($_products). If you need to get a new product collection there are several ways, but the most general is Mage::getResourceModel('catalog/product_collection')->setStoreId($storeId)