0

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

1

Hard to tell what you're trying to achieve. If you want to add the filtering from your previous code to the category filtered collection and remove the surplus stuff introduced by that alien code, you'll have to merge like this:

$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' => $productCollection->getTable('catalog/category_product_index')),
        implode(' '.Zend_Db_Select::SQL_AND.' ', $joinConditions),
        array('cat_index_position' => 'position', 'cat_id' => 'category_id')
    )
    ->order('cat_index.category_id')
;

$productCollection
    ->addAttributeToFilter('status', 1)
    ->addAttributeToSelect(array('name', 'price', 'small_image'))
    ->addUrlRewrite();

foreach ($productCollection as $item) {
    $categoryId = $item->getCatId();
    echo $item->getName();
}

If it's the grouping that needs to be done, leave a comment – and bear in mind that grouping may yield unwanted results since a product can belong to more than one category.

  • Hey. Thank you for the answer. I tried to use you answer, but unfortunately I have an error. Check my updated question. – Mark Nov 11 '16 at 22:55
  • 1
    see updated answer. there are two changes: $productCollection->getTable and implode(' AND ', $joinConditions). the previous join stopped working, maybe due to a change to magento core. – pong Nov 11 '16 at 23:58
  • Boooooom. Wonderful!!! Thank you very very much. Just last question, if I want to group the products by category in the list? Because I need to add the category name at the top of each group and then, from a menu, click on a link and scroll the list – Mark Nov 12 '16 at 0:02
  • 1
    updated answer: product collection is sorted by category id, and inside foreach you can access the latter for further processing – pong Nov 12 '16 at 0:37
  • 1
    Hey, I can't thank you enough for the help. You great. – Mark Nov 12 '16 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.