If you only have the category ID and do not want to load the category itself, you can use this code (given category id $categoryId
):
$category = Mage::getModel('catalog/category')->setId($categoryId);
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($category)
->getAllIds();
This code respects the is_anchor attribute because the filter is using the index, so as a result it returns all products that Magento would show on front-end, including products from the underlying sub-categories.
Not without custom code. There is a block class for the filter display, Mage_Catalog_Block_Layer_State
, but the filter data comes from the model Mage_Catalog_Model_Layer_State
, which is populated by Mage_Catalog_Model_Layer
and cannot be influenced from the block.
If your alternative sort order can be expressed with an SQL order clause, you could override Mage_Catalog_Model_Layer::_prepareAttributeCollection()
and change the sort order of the collection depending on the current category. It's set to $collection->setOrder('position', 'ASC')
before this method is called. But rewriting the layout model is better avoided.
A better option without rewrites is to create a custom block that extends Mage_Catalog_Block_Layer_State
:
class Stack_Catalog_Block_Layer_State extends Mage_Catalog_Block_Layer_State
{
public function getActiveFilters()
{
$filters = parent::getActiveFilters();
// $filters is an array of Mage_Catalog_Model_Layer_Filter_Item objects
// sort it with usort() to your needs
return $filters;
}
}
And another one that extends Mage_Catalog_Block_Layer_View
because this is where the state block is created:
class Stack_Catalog_Block_Layer_View extends Mage_Catalog_Block_Layer_View
{
protected function _initBlocks()
{
parent::_initBlocks();
$this->_stateBlockName = 'stack_catalog/layer_state';
}
}
Then replace the layer block in your categories custom layout update:
<reference name="left">
<block type="stack_catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
It's adding a new block, but using the same name catalog.leftnav
of the original block, so it will be replaced. This is because you cannot change the type of an existing block.
Best Answer
That looks like an issue with the test question to me.
I would have thought the correct answer should be option 2:
If is_anchor = yes, all the products for the current category and all children categories display, along with the layered navigation if filterable attributes are setup allowing you to filter the products.