Adding a Static Block AFTER Magento Product List
As a senior developer specializing in e-commerce platforms, I often encounter questions about customizing Magento’s layout. One common request is to insert a static block after the product list on category pages. In this post, we’ll explore how to achieve this using XML layouts and provide step-by-step instructions for adding a block below your products.
Understanding Magento Layout Structure
Magento uses an XML-based layout system to define the structure of its pages. The core layout file is app/design/frontend/base/default/layout/page.xml. Most customizations involve creating new XML files in your theme’s layout folder (usually found at app/design/frontend/your_vendor/your_package/layout).
To add a static block after a product list, you need to understand how Magento constructs category pages. The core category layout is defined in the file mentioned above and includes the following section:
<catalog_category_layered>
<reference name="content">
<block type="catalog/category_view" name="category.products" as="products" template="catalog/category/view.phtml"/>
</reference>
</catalog_category_layered>
The category.view block contains the product list for a category. To add content after this block, you’ll need to create a new XML file and reference it in your theme’s layout.
Creating an XML File to Add Your Block
-
Navigate to your Magento theme directory:
app/design/frontend/your_vendor/your_package/layout/. -
Create a new XML file, e.g.,
catalog_category_layered_after_products.xml. -
Open the newly created file and add the following content:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layouts.xsd">
<catalog_category_layered>
<referenceBlock name="category.products.after">
<block type="core/text_list" name="after_products_block" as="after_products">
<action method="setId"><argument name="id">after-products</argument></action>
<action method="setArea"><argument name="area">frontend</argument></action>
</block>
</referenceBlock>
</catalog_category_layered>
</layout>
In this file, we’re creating a new referenceBlock named category.products.after. Within it, we set up a core/text_list block with the ID after-products.
Adding Your Static Block
- Now, let’s create the static block that you want to display after your product list.
- Navigate to Content > Static Blocks in your Magento admin panel.
- Click on Add New Block.
- Set a unique identifier (e.g., “after_products_block”).
- Add your desired content (HTML, text, or shortcodes).
- Save the block.
Connecting It All Together
-
Back to your theme layout directory (
app/design/frontend/your_vendor/your_package/layout/), create a new XML file namedcatalog_category_layered.xml. -
Open this new file and add the following content:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layouts.xsd">
<catalog_category_layered>
<referenceBlock name="category.products.after" after="-">
<block class="Magento\Cms\Block\Block" name="after_products_block">
<arguments>
<argument name="block_id" xsi:type="string">after_products_block</argument>
</arguments>
</block>
</referenceBlock>
</catalog_category_layered>
</layout>
In this file, we’re referencing the category.products.after block we created earlier. We then add a new block with the class Magento\Cms\Block\Block. This is where you specify your static block ID (after_products_block).
- Save both XML files and clear your Magento cache: System > Cache Management.
Conclusion
Adding a static block after the product list in Magento involves creating custom XML layouts and referencing them correctly. By following these steps, you can insert content below your product listing on category pages without touching core files.
Remember to test your implementation thoroughly, especially if you’re running multiple themes or have complex layout hierarchies. Always keep track of your changes using version control systems like Git for easier debugging and rollback if needed.
For more advanced customizations and best practices in Magento development, consider exploring our team’s insights at LaravelCompany.com.
This guide demonstrates the power of Magento’s layout system while keeping everything clean and maintainable within your theme. Happy coding!