Displaying Specific Products at the Top of a Magento Category
Introduction
As an e-commerce platform, Magento provides extensive flexibility for managing your product catalog. One common requirement is to display specific products at the top of a category pageâoften referred to as “special” or “featured” products. In this post, we’ll walk through how to achieve this manually and programmatically.
Manually Setting Special Products in the Backend
Step 1: Create a new attribute for special products
- Navigate to Catalog > Attributes.
- Click on “Add New Attribute.”
- Enter the following details:
- Attribute Code:
special_product - Frontend Input Type: Dropdown
- Attribute Code:
- Save the new attribute.
Read more about creating custom attributes in Magento.
Step 2: Assign special status to products
- Navigate to Catalog > Products.
- Select the product you want to make ‘special.’
- In the Product Details section, find the
Special Productattribute you created and set it to ‘Yes’. - Save the changes.
Displaying Special Products via a Static Block
Step 1: Create a new static block
- Navigate to CMS > Static Blocks.
- Click on “Add New Block.”
- Enter the following details:
- Identifier:
category_special_products - Title: (Any title you prefer)
- Identifier:
- Save the new block.
Step 2: Add content to the static block
- Edit the newly created static block.
- In the Content section, enter the following code:
<?php $specialProducts = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('entity_id', array('in' => explode(',', '1,2,3')));
if (count($specialProducts) > 0): ?>
<ul>
<?php foreach ($specialProducts as $product): ?>
<li><a href="<?php echo $this->getProductUrl($product); ?>"><?php echo $product->getName(); ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This code filters the product collection based on the entity_id values passed as a comma-separated string (in this example, 1,2,3).
Step 3: Place the static block below category description
- Edit your category’s page layout using XML.
- Add the following line inside the
<reference name="content">node:
<block type="cms/block" name="category_special_products" before="-">
<action method="setBlockId"><block_id>category_special_products</block_id></action>
</block>
Conclusion
Displaying specific products at the top of a Magento category page is a straightforward process once you understand how to create custom attributes and work with static blocks. By following this guide, you can manually assign special status to your products in the backend or programmatically via a static block.
Remember, for complex scenarios involving multiple categories or more dynamic selection criteria, consider implementing it as an extension using Magento’s API and event-observer model. This approach provides greater scalability and flexibility.
If you’re looking to streamline your Magento development workflow, check out our guide on best practices for Magento 2 development.