2

When grabbing the product id's from a category where the is_anchor attribute is set we get the complete collection. Including products from underlying categories.

Question: How can we get a list/array of product id's for any given category id that has the is_anchor attribute set - where the collection contains only the real products from that category only (and not also products from underlying categories)

This is what we have to date. But I am sure this is not the fastest method.

$category = new Varien_Object(array('id'=>$_cat));
$positions = Mage::getResourceModel('catalog/category')->getProductsPosition($category);
$includeIDs = array_keys($positions);

Reference: Array of product ids for a given category (respecting is_anchor)

1
  • this is so far the first way I have found to get products for a categoryid not in the current site's tree, too. it feels very diagonal but it works where 10 other things have failed...
    – iphigenie
    Mar 1, 2018 at 8:15

2 Answers 2

1

I think the fastest way (performance) to get only an array containing the product ids for a category is to build your own query such as :

$resource = Mage::getResourceModel('core/resource');
$adapter = $resource->getReadConnection();
$product_ids = $adapter->fetchCol(
    $adapter->select()
        ->from(
            $resource->getTable('catalog/category_product'),
            array('product_id')
        )
        ->where('category_id = ?', $cat)
);
0

I think the only alternative is to join catalog_category_product into the product collection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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