Displaying Attribute Values on Category Pages in Magento
When it comes to customizing your Magento store’s category pages, one common requirement is to display attribute values for the products currently visible on the page. In this post, we’ll guide you through displaying a dropdown brand attribute on category pages.
Firstly, let’s understand what we have: You’ve created a Brand attribute of type Dropdown and populated it with 5-6 options. Now, you want to display these branded options as filters on the category page where only products assigned to those brands will be visible.
Accessing Category Products
To achieve this, we’ll start by accessing the current category’s product collection in your Magento template file (category.phtml). Here’s how:
<?php
$currentCategory = $this->getCurrentCategory();
$products = $currentCategory->getProductCollection()->addAttributeToSelect('*');
?>
In this code snippet, we first fetch the current category using $this->getCurrentCategory(). Then, we create a product collection for that category and select all attributes using ->addAttributeToSelect('*').
Accessing Brand Attribute
Next, let’s access the brand attribute from our product collection:
<?php foreach ($products as $product): ?>
<?php $brand = $product->getAttributeText('brand'); ?>
<!-- Your code here -->
<?php endforeach; ?>
We use $product->getAttributeText('brand') to fetch the brand name. This function returns the frontend label for the selected attribute value.
Displaying Brand as Filter
Now, let’s display these brands as filters:
<div class="category-filters">
<?php $brands = array_unique($products->getColumnValues('brand')); ?>
<?php foreach ($brands as $brand): ?>
<div>
<input type="checkbox" id="<?php echo $brand; ?>" name="brand[]" value="<?php echo $brand; ?>">
<label for="<?php echo $brand; ?>"><?php echo $brand; ?></label>
</div>
<?php endforeach; ?>
</div>
In this code:
- We use
array_uniqueto ensure each brand appears only once. - For each unique brand, we create a checkbox with the same name (
brand[]) so that multiple brands can be selected simultaneously. The value of the checkbox is set to the brand name.
Implementing Brand Filter Functionality
To make these filters functional, you’ll need to implement the logic in your category controller or observer. When a user selects a brand filter, update the product collection to only include products with that specific brand.
This involves creating an observer for controller_action_predispatch_catalog_category_view and filtering the product collection based on selected brands:
<?php
public function execute(\Magento\Framework\Event\Observer $observer)
{
$action = $observer->getControllerAction();
$request = $action->getRequest();
if ($request->has('brand')) {
$brands = $request->getParam('brand');
// Filter products based on selected brands
$category = $action->helper('Magento\Framework\Data\Helper\Post::filterCategoryByAttributes')(
$category,
'simple',
array('brand' => explode(',', implode(',', $brands)))
);
}
}
Conclusion
Displaying attribute values like brand as filters on category pages in Magento requires a combination of template manipulation and controller logic. By following the steps outlined above, you can effectively create dynamic brand filters that only show products matching the selected brands.
Remember to test your implementation thoroughly across different browsers and devices to ensure a seamless user experience for your customers.
For more advanced customizations like this, consider exploring our Magento development services at Laravel Company. Our expert team can help you tailor your Magento store to meet your specific business needs.