I am trying to retrieve a list of product from an array of specific categories IDs and group by its category.
In this way, I retrieve all product in the root Category:
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('status', 1) // enabled
->addAttributeToSelect(array('name', 'price', 'small_image'))
->addUrlRewrite();
foreach ($collection as $item) {
echo $item->getName();
}
Then I saw in another post, that it is possible to get products from specific category, in this way:
$storeId = Mage::app()->getStore()->getId();
$categoryIds = $catIds; Array = [23,24,25,27,30]
$productCollection = Mage::getModel('catalog/product')->getCollection();
$joinConditions = array(
'cat_index.product_id=e.entity_id',
$productCollection->getConnection()->quoteInto('cat_index.category_id IN (?)', $categoryIds),
$productCollection->getConnection()->quoteInto('cat_index.store_id=?', $storeId)
);
$productCollection->getSelect()->join(
array('cat_index' => $this->getTable('catalog/category_product_index')),
$joinConditions,
array('cat_index_position' => 'position')
);
$productCollection
->addStoreFilter()
->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addUrlRewrite();
But unfortunately I cannot adapt this last code to my initial one. Any help would be appreciated a lot.
UPDATE
This is what I have now:
$storeId = Mage::app()->getStore()->getId();
$categoryIds = array('42','43','44','45','46','47');
$productCollection = Mage::getModel('catalog/product')->getCollection();
$joinConditions = array(
'cat_index.product_id=e.entity_id',
$productCollection->getConnection()->quoteInto('cat_index.category_id IN (?)', $categoryIds),
$productCollection->getConnection()->quoteInto('cat_index.store_id=?', $storeId)
);
$productCollection->getSelect()->join(
array('cat_index' => $this->getTable('catalog/category_product_index')),
$joinConditions,
array('cat_index_position' => 'position')
);
$productCollection
->addAttributeToFilter('status', 1)
->addAttributeToSelect(array('name', 'price', 'small_image'))
->addUrlRewrite();
foreach ($productCollection as $item) {
echo $item->getName();
}
Unfortunatelly I have an error:
THERE HAS BEEN AN ERROR PROCESSING YOUR REQUEST Exception printing is disabled by default for security reasons.
Error log record number: 1276294659917