Displaying Category Descriptions Above and Below Product Listing in Magento 2
Introduction
In Magento 2, displaying category descriptions both above and below the product listing is a common requirement for e-commerce stores looking to optimize their content strategy. By separating the description into short (above) and long (below) versions, you can provide users with a quick overview of the category while also optimizing for search engines.
Problem Description
The out-of-the-box Magento 2 functionality does not support this feature directly. The built-in “Description” field is displayed only once, either above or below the products based on your theme settings. Additionally, the existing marketplace extension Second Category Description appears to be broken and may not provide a reliable solution.
Understanding WooCommerce Approach
If you’re familiar with WooCommerce (WordPress), you might have used the explode() function to split the category description into two parts. This approach is efficient because it doesn’t require creating new database fields or modifying your theme structure significantly. However, Magento 2 has a different architecture and requires a more structured solution.
Solution: Extend the Category Model
To achieve this in Magento 2, you will need to extend the Magento\Cms\Model\Block model to add additional functionality for splitting category descriptions. Here’s a step-by-step guide:
- Create an extension that inherits from the
Magento_Cmsmodule.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="YourCompany_SecondCategoryDescription"/>
</config>
- Add the necessary dependencies and setup files.
<dependencies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Component/etc/module/dependency-xmlschematorule.xsd">
<module name="Magento_Cms"/>
</dependencies>
- Override the
Magento\Cms\Model\Blockclass to add a new attribute for the second category description.
// YourCompany/SecondCategoryDescription/Model/Block.php
namespace YourCompany\SecondCategoryDescription\Model;
class Block extends \Magento\Cms\Model\Block
{
protected function _construct()
{
$this->_init('YourCompany\SecondCategoryDescription\Model\ResourceModel\Block');
}
}
- Create a resource model and setup the additional attribute in your schema file.
// YourCompany/SecondCategoryDescription/etc/db_schema.xml
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Component/etc/schema.xsd">
<table name="cms_block" install="true">
<column name="second_description" type="text" nullable="true"/>
</table>
</schema>
- Update your theme to display the separated descriptions.
<!-- app/design/frontend/Vendor/theme/Magento_Catalog/templates/category/view.phtml -->
<div class="category-description">
<?php echo $block->getSecondDescription(); ?>
</div>
<div class="products">
<!-- Product listing -->
</div>
<div class="category-description-below">
<?php echo $block->getDescription(); ?>
</div>
Conclusion
While Magento 2 doesn’t support this feature out-of-the-box, you can achieve the desired functionality by extending the core Magento_Cms module and adding a new attribute to store the second category description. This approach allows for a clean integration without modifying your theme’s existing structure.
Remember that when working with Magento 2, it’s essential to follow best practices such as proper file organization, version control, and keeping your codebase up-to-date. For more information on Magento 2 development, consider visiting our blog at https://laravelcompany.com/magento-2-development.