How do I get the minimal price of a grouped product from a product collection?
-
16-10-2019 - |
Question
I'm loading all products using the product collection resource model.
$_categoryId = $this->getCategoryId();
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addCategoryFilter(Mage::getModel('catalog/category')->load($_categoryId))
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
I'm however not able to display the minimal price of grouped products using for example.
echo $_productCollection->getFirstItem()->getMinimalPrice();
This does work for simple products. This seems to be related to the fact that this is a core/template block type and not a catalog/product_list block. But using catalog/product_list is not an option due to other reasons.
Solution
If you update your code so it does no just select * but instead use the catalog config attributes then your code works for all product types.
Mage::getSingleton('catalog/config')->getProductAttributes()
Your code should look like.
$_categoryId = $this->getCategoryId();
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addCategoryFilter(Mage::getModel('catalog/category')->load($_categoryId))
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
In this case the correct minimal prices will be loaded for each type of product.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange