Magento: How to retrieve the minimal price of (i.e.) grouped products?

magento

Solution

I looked a little deeper into Magento's code and tracked down how Magento loads the products of the product list. When viewing the product list, the product collection is loaded by the model Mage_Catalog_Model_Layer, which adds the minimal price and other stuff to the products in the collection by adding joins to the collection’s SQL, but I have not yet found a model that does the same thing for single products instead of collections.

Because I don’t want to use a direct SQL query, I checked out the CatalogIndex module, but that module seems not to be working at all because all its indexing tables are empty or even corrupted.

$data_grouped = Mage::getModel("catalogindex/data_grouped");
$minimal = $data_grouped->getMinimalPrice(array($_product->getId()), Mage::app()->getStore());

looked good at first but it gives me the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catalog_product_index_price.value' in 'field list'

The CatalogIndex module is either deprecated or I’m just too stupid to enable it.

However, I have now created a workaround in my price.phtml template, which uses the same method to retrieve the minimal price and tax percents as the product list does: At the beginning of the template I added the following:

$this->setProduct(
    Mage::getModel("catalog/product")->getCollection()
        ->addAttributeToSelect(Mage::getSingleton("catalog/config")->getProductAttributes())
        ->addAttributeToFilter("entity_id", $this->getProduct()->getId())
        ->setPage(1, 1)
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->load()
        ->getFirstItem()
);

which may not be the perfect solution, but it does the job and it does it a little cleaner than iterating over all child products and finding the minimal price that way.

Problem

I am trying to display a grouped product's price on the product view page in Magento 1.7.0.2, just as it is being displayed in the category product listing ("Starting at: xx.xx"). I thought I could just use ``` $this->getPriceHtml($_product, true); ``` to do so because it works the same way in the category product listing, but as everything I tried to do in Magento, it is not that easy because that method doesnt return anything on the product view page. I looked a little deeper into Magento's code and figured out that the cause of this problem is the product's minimal price data not being set on the product view page ($_product->getMinimalPrice() returns null). Therefore I did some research on how to load a product's minimal price which brought up ideas like ``` $_product->getPriceModel()->getMinimalPrice($_product); ``` which doesnt work because apparently that method was deprecated and removed in one of the last updates or ``` $priceModel = Mage::getResourceModel('catalogindex/price'); $priceModel->setStoreId(Mage::app()->getStore()->getId()); $priceModel->setCustomerGroupId(Mage::getSingleton('customer/session')->getCustomerGroupId()); $minimalPrices = $priceModel->getMinimalPrices(array($_product->getId())); $minimalPrice = $minimalPrices[0]; $_product->setData('minimal_price', $minimalPrice['value']); $_product->setData('minimal_tax_class_id', $minimalPrice['tax_class_id']); ``` which doesnt work either because the table 'catalogindex_minimal_price' is empty. So my question is, how does Magento load the minimal price in the category product listing?

Original source

Related problems