Displaying Filterable Designer Attributes Outside of Layered Navigation
In Magento, layered navigation is an excellent feature for enhancing user experience by allowing customers to filter products based on various attributes like color, size, price, and custom attributes like ‘designer’. However, if you want to display this designer attribute list outside the category page in a dedicated template file, you’ll need to use Magento’s collection manipulation capabilities effectively. Let me guide you through the process.
Understanding the Current Approach
Your current code is correctly loading all products and filtering them by visibility (only showing both visible and searchable products). You’re also successfully filtering for products that are currently in stock using the cataloginventory/stock_status join field. The issue lies in your attempt to filter by category using $collection->addCategoryFilter(Mage::getModel('catalog/category')->load($store_category));.
The problem with this approach is that Magento’s collection filtering works by matching product categories against a specific attribute like category_id, not the root category ID as you’re passing. To fix this, we’ll need to use a different strategy.
A Better Way: Using Category Product Collection
Instead of trying to filter directly on the $collection object, let’s create a new collection that represents all products within your specific store category. This approach ensures that only relevant products are considered for the designer attribute filtering.
Refactored Code Example
Let me refactor your code to demonstrate this:
// Load the current category ID
$store_category = Mage::app()->getStore()->getRootCategoryId();
// Create a new product collection filtered by the store's root category
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->joinField(
'stock_status',
'cataloginventory/stock_status',
'stock_status',
'product_id=entity_id',
array(
'stock_status' => Mage_CatalogInventory_Model_Stock_Status::STATUS_IN_STOCK
)
);
// Add the designer attribute to the product selection (important for grouping later)
$collection->addAttributeToSelect('designer');
// Filter products by the store's root category directly in the collection object
$collection->addCategoryFilter(Mage::getModel('catalog/category')->load($store_category));
// Group the remaining results by the designer attribute, effectively limiting to only products with a designer set
$collection->groupByAttribute('designer');
echo 'Collection count: '.$collection->count();
What I Changed
- Created a new collection from
Mage::getModel('catalog/product')->getCollection()instead of trying to filter the existing one. - Applied the
addCategoryFilterdirectly on the new collection, ensuring it only includes products from your specific store category. - Ensured that ‘designer’ is added as an attribute for selection before grouping.
Conclusion
By creating a dedicated collection filtered by your desired category and then grouping by the designer attribute, you can achieve the desired list of designers within the context of your store’s specific category. This approach ensures that only relevant data is processed, improving performance and precision in your results.
Remember to place this code inside an appropriate template file (like app/design/frontend/{your_package}/{your_theme}/template/page/designerlist.phtml), call it using <?php echo $this->getLayout()->createBlock('core/template')->setTemplate('page/designerlist.phtml')->toHtml(); ?> in your layout XML, and ensure the required permissions are set for the template file.