Creating Landing Pages by Attribute for Magento
Dear Jaro,
Thank you for reaching out about this challenge of creating landing pages filtered by attributes in Magento. I understand your requirement to display products based on specific attribute values while maintaining layered navigation. Let’s explore some solutions and best practices for achieving this.
Understanding the Problem: Filtering Products by Attributes
In Magento, the default approach of using advanced search doesn’t provide layered navigation out-of-the-box. While the FishPig module you found offers a solution, manually configuring thousands of attribute combinations in the database can become unmanageable as your catalog grows.
Your idea of reviewing the catalog/layer model is insightful, but modifying core Magento models directly could lead to compatibility issues and make future upgrades more complex.
A Better Approach: Custom Module Development
To balance your requirements without resorting to extensive database configuration or core modifications, I recommend developing a custom module. This approach allows you to extend Magento’s functionality while keeping your codebase clean and upgrade-friendly.
Here’s an outline of the steps involved in creating a custom module:
Step 1: Define Your Attribute Filter
Identify the specific attribute (e.g., Brand) you want to use for filtering products on the landing page. Make sure this attribute is configured correctly in Magento with appropriate options.
Step 2: Create a New Controller Action
Create a new controller action that handles the landing page request. In your module’s controllers directory, add a new file (e.g., IndexController.php) and define an action to fetch products filtered by the specified attribute.
Here’s a basic example:
namespace YourCompany\YourModule\Controller\Index;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\ResponseInterface;
class Index implements ActionInterface
{
public function execute(): ResponseInterface
{
$brand = 'Addidas'; // Replace this with the desired attribute value
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $this->_productCollectionFactory->create();
$productCollection->addAttributeToFilter('brand', $brand)
->setPageSize(10);
// Load the filtered products
// Render your landing page view with the fetched products
}
}
Step 3: Create a Custom Layout File
To ensure that layered navigation is enabled for your custom landing pages, you need to create a new layout XML file. This file will specify the use of Magento’s default layer component.
In your module’s view/frontend/layout directory, add a new file (e.g., catalog_product_index_index.xml) and define the necessary components:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="catalog_product_index_layered"/>
<referenceBlock name="product_list" remove="true"/>
<block type="Magento\Catalog\Block\Product\ListProduct" name="custom_product_list"
as="product_list" template="YourCompany_YourModule::product/list.phtml">
<action method="setCollection">
<argument name="collection" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Collection</argument>
</action>
</block>
</page>
Step 4: Create the Landing Page Template
Design your landing page template (e.g., product/list.phtml) to display the filtered products and include Magento’s layered navigation component.
Conclusion: Building a Robust, Extensible Solution
By developing a custom module that extends Magento’s functionality without modifying core files or relying on extensive database configuration, you can create landing pages filtered by attributes while maintaining layered navigation. This approach provides a clean, upgrade-friendly solution for managing your catalog effectively.
Remember to follow Magento development best practices when creating your custom module:
- Keep your code organized and well-commented.
- Use dependency injection principles for better testability.
- Validate your data before processing it.
- Conduct thorough testing before deployment.
I hope this guide helps you create the perfect landing pages for your client’s needs. If you have any further questions or need more detailed code examples, please don’t hesitate to ask. We’re always here to support your Magento development journey at Laravel Company.
Happy coding!
Best regards, [Your Name] Senior Developer & Technical Blogger Laravel Company