Disable Clicking of a Single Parent Category Button in Magento Navigation Menu
As an experienced e-commerce developer, I often encounter situations where clients need to restrict certain navigation options in their Magento stores. One common requirement is to disable the click functionality of specific parent category buttons while keeping the subcategories accessible.
In this post, we’ll explore how to achieve this using a combination of core Magento features and customization. The solution involves modifying the block template for the top menu and adding a custom observer to conditionally disable the click event based on the current category ID.
Understanding the Requirement
The primary goal is to prevent users from navigating directly to a specific parent category while still allowing them to access its child categories. This could be useful in scenarios where certain high-level categories are meant for internal use or when you want to restrict direct navigation to particular sections of your store.
Solution Overview
To disable the click event for a single parent category, we’ll follow these steps:
- Identify the category ID(s) that need to be restricted.
- Create a custom observer module to conditionally enable/disable the menu items based on the category ID.
- Modify the core block template file responsible for rendering the top menu.
- Implement the necessary logic in the template file and observer.
Implementing the Custom Observer
First, let’s create a custom observer that checks if the current category matches the restricted parent categories (with IDs 123 and 456 in this example).
// app/code/Vendor/DisableClickObserver/etc/event_types.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:event:etc_event.xsd">
<event name="catalog_block_product_list_get_products">
<observer name="disable_click_observer" instance="Vendor\DisableClickObserver\Model\Observer"/>
</event>
</config>
// app/code/Vendor/DisableClickObserver/Model/Observer.php
namespace Vendor\DisableClickObserver\Model;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Registry;
class Observer implements ObserverInterface
{
private $registry;
public function __construct(Registry $registry)
{
$this->registry = $registry;
}
public function execute(\Magento\Framework\Event\Observer $observer): void
{
$categoryId = $this->registry->registry('current_category')->getId();
if (in_array($categoryId, [123, 456])) {
// Disable click event for the restricted category IDs
$block = $observer->getBlock();
$block->setData('disable_click', true);
}
}
}
In this example, we’re using the catalog_block_product_list_get_products event to inject our observer logic when rendering the product list. The current category ID is obtained from the registry and checked against the restricted IDs.
Modifying the Core Block Template
Next, let’s modify the core block template file responsible for rendering the top menu. In this case, we’ll work with vendor/magento/module-catalog/view/frontend/templates/category/tree.phtml.
<!-- app/design/frontend/Vendor/theme/Magento_Catalog/templates/category/tree.phtml -->
<ol class="items">
<?php foreach ($children as $child): ?>
<li>
<?php if (!$this->catalogCategoryHelper->canShowCategory($child)): ?>
<a href="<?php /* @escapeNotVerified */ echo $child->getUrl() ?>"
class="category-link"
data-container-width="100%"
data-container-height="auto">
<h3><?php /* @escapeNotVerified */ echo $child->getName(); ?></h3>
</a>
<?php else: ?>
<span class="category-name" style="<?php if ($block->getData('disable_click')): ?>cursor: default;<?php endif ?>">
<span class="category-label"><?php /* @escapeNotVerified */ echo $child->getName() ?></span>
</span>
<?php endif ?>
<?php if (count($child->getChildren())): ?>
<ol style="<?php if ($block->getData('disable_click')): ?>display: none;<?php endif ?>">
<?php /* @escapeNotVerified */ echo $this->getChildHtml('category.tree') ?>
</ol>
<?php endif ?>
</li>
<?php endforeach ?>
</ol>
In this modified template, we’ve added a conditional check for the restricted categories using the disable_click data set by our observer. For these restricted categories, we change the cursor style to default, which visually indicates that the link is not clickable.
Conclusion and Best Practices
Disabling the click event for specific parent category buttons in Magento requires a combination of customization techniques, such as creating an observer module and modifying core block templates.
This solution demonstrates how to target specific category IDs while keeping subcategories accessible. Remember to replace 123 and 456 with your actual restricted category IDs.
As best practices for Magento development:
- Always create a custom theme or extension rather than modifying the core files directly.
- Use event observers sparingly and ensure they have minimal performance impact on your storefront.
- Test the changes thoroughly before deploying them to a live environment.
- Regularly update your Magento installation to benefit from security patches and performance improvements.
For more advanced customization techniques and Magento best practices, consider exploring our guide on Magento development guidelines at Laravel Company.