Hiding Configurable Products from Magento Catalog and Search
As an experienced Magento developer, I often receive queries about managing the visibility of complex product types like configurable items. One common request is to display only simple products associated with a configurable SKU while keeping the parent configurables hidden from both the catalog view and search results.
Let’s dive into a practical solution for this scenario.
The Challenge: Simple Products Redirecting
In your case, when a customer selects a simple product via layered navigation (like Product A in red or blue), they are being redirected to the configurable product page. This is because by default, Magento links simple products to their parent configuration.
The goal is to keep only the selected simple item visible and maintain a clean user experience without exposing the underlying complex structure.
The Solution: Overriding Catalog Controller
To achieve this, we’ll need to override the catalog_product_view action in the catalog/productController.php file. This controller handles the display of product pages.
Here’s a step-by-step approach:
-
Create an extension: First, create a custom module under
app/code/YourVendor/YourModule. Make sure the namespace and local name are correct. -
Override the controller: In your module, navigate to
controller/Catalog/ProductController.phpand override theviewAction()method.
public function viewAction()
{
$productId = $this->getRequest()->getParam('id');
$product = $this->_initProduct();
if (!$product) {
return false;
}
// Check if the product is part of a configurable set
if ($product->isTypeConfigurable()) {
$simpleProducts = $product->getTypeInstance(true)->getUsedProducts($product);
foreach ($simpleProducts as $simpleProduct) {
if ($simpleProduct->getId() == $productId) {
// Display only the selected simple product
$this->_forward('view', 'catalog/product', null, ['id' => $simpleProduct->getId()]);
return;
}
}
}
return parent::viewAction();
}
In this code snippet:
- We retrieve the requested product ID.
- If the product is of type configurable, we fetch all associated simple products.
- We then compare the IDs to find a match with the selected simple item. If found, we forward to that specific simple product page, effectively hiding the parent configuration.
Enabling the Extension
To ensure your override takes effect:
- Enable the module in
app/etc/config.xml:
<config>
<modules>
<YourVendor_YourModule>
<active>true</active>
<codePool>core</codePool>
</YourVendor_YourModule>
</modules>
</config>
- Flush your cache and apply any necessary database structure updates.
Conclusion
By implementing this approach, you can effectively manage the visibility of configurable products in Magento while maintaining a streamlined user experience for customers browsing via layered navigation or search results.
Remember to always keep an eye on Magento’s official documentation for the most up-to-date practices: Magento 2 Development Documentation.
For further insights on Magento extension development, feel free to explore my recent posts at https://laravelcompany.com/magento-development-tutorials.