2

There is an array of category ids i.e. array(18,19,25);

I want to get those products which has all these categories from above array. Product should present in 18,19 and 25.

Array values can be anything i.e. category ids.

  • You mean that you want to get an array of products which exist in categorys 18, 19 AND 25? Or do you want to get them if they are in any of 18, 19 and 25? – John Cuthbert Nov 13 '15 at 16:24
  • That product should present in 18,19 and 25. – ved Nov 13 '15 at 17:06
  • that means both 3 categories ? If product is in one category then what will be happen. – Amit Bera Nov 14 '15 at 12:36
  • 1
    Then that product should not be displayed. Product should present in all categories from the array – ved Nov 14 '15 at 12:47
  • You should extend little bit product collection. Check this answer – Alex Silenko Jul 31 '17 at 10:41
1

Let's Assume you have categoryIds Array

$categories = array(18,19,25);

First You have to do category join

$collection->joinField(
    'category_id', 'catalog/category_product', 'category_id', 
    'product_id = entity_id', null, 'left'
);

Now add simple IN condition of collection

$pCollection->addAttributeToFilter('category_id', array('in' => implode(',', $categories)));
0

try

$productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('category_id', array('in' => array('finset' => '18','19','25')));
  • please join with category product table.. you may be miss it – Amit Bera Nov 13 '15 at 18:01
  • Can you give me entire query.. @amit bera – ved Nov 14 '15 at 10:35
  • This produces an uncaught Mage_Eav_Exception: Invalid attribute name: category_id – Liam McArthur Sep 27 '18 at 12:43
0

This can be a bit scary but is working fine for me, with:

  • Magento 1.9
  • Flat Table
// An array of category ids
$categoryIds = [18,19,25];

// Get a collection of products
$collection = Mage::getModel('catalog/product')->getCollection();

// Add a filter for each category: only products belonging to each
// category will be in the collection
$i = 1;
foreach ($categoryIds as $categoyId) {
  $collection->joinField(
    "category_product_${i}",
    'catalog/category_product', 
    null, 
    'product_id=entity_id',
    "{{table}}.category_id=${categoyId}", 
    'inner'
  );
  $i++;
}

// Set distinct (avoid duplicate products)
$collection->distinct(true);
0

Please try it

$catIds = array('10','82');
$productCollection = 
Mage::getResourceModel('catalog/product_collection')
->setStoreId(0)
->joinField('category_id', 'catalog/category_product', 'category_id', 
'product_id=entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' =>  array('finset' =>
$catIds )))
->addAttributeToSelect('*');

$productCollection->getSelect()->group('product_id')->distinct(true);
-1
$collection = Mage::getModel('catalog/product')
              ->getCollection()
              ->addAttributeToSort('created_at', 'DESC')
              ->load();
   foreach ($collection as $product){
       echo $product->getId().'</br>';
       echo $product->getName().'</br>';
   }
-1
$products = Mage::getResourceModel('catalog/product_collection');
foreach($category_ids as $id)
    $products->addCategoryFilter(Mage::getModel('catalog/category')->load($id));

UPDATE: this answer is wrong - it will filter the collection by the last category from $category_ids array - addCategoryFilter method doesn't support stacking. I didn't know that - what is the point of making a filter that supports only one category?

  • This loads products with an OR logic (products in a category or in a another category), but the question was about load product with an AND logic – Andrea Apr 13 '17 at 15:32
  • 1
    @Andrea we are both wrong. addCategoryFilter when applied multiple times takes into account only the last category. so my code will filter by the last category id in $category_ids. – Konstantin Gerasimov Apr 13 '17 at 17:02
  • Ah, ok, I didn't know it :) I found a way to do this by building a query with multiple joins. I added an answer. – Andrea Apr 13 '17 at 17:28

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.