Adding Custom Menu Item to Sales Sidebar in Magento 2
As a developer new to Magento 2, you might find yourself needing to customize the admin sidebar to fit your specific needs. One common requirement is adding a custom menu item to an existing sidebar tab, such as the Sales tab.
In this post, we’ll guide you through the process of creating a custom menu item for the Sales sidebar and linking it to a new page.
Understanding Magento 2 Sidebar Structure
Magento 2 uses XML files to define the admin panel layout. The main file responsible for the sidebar structure is app/code/Magento/Backend/etc/adminhtml/menu.xml.
For our purpose, we’ll be focusing on the sales tab. Here’s how it looks in the menu.xml file:
<menuItem name="Magento_Sales::sales" sortOrder="50">
<title translate="true">Sales</title>
...
<children>
<menuItem name="Magento_Sales::custom_item" sortOrder="10">
<title translate="true">Custom Item</title>
<action path="vendor_module/routename/index"/>
</menuItem>
</children>
</menuItem>
Creating a Custom Menu Item
Follow these steps to add your custom menu item:
-
Create the XML File: In your custom module, navigate to
app/code/Vendor/Module/etc/adminhtml/menu.xml. If the folder doesn’t exist, create it. -
Add the Menu Item:
<menuItem name="Vendor_Module::custom_item" sortOrder="20">
<title translate="true">Custom Tab</title>
<children>
<menuItem name="Vendor_Module::new_page" sortOrder="10">
<title translate="true">New Page</title>
<action path="vendor_module/newpage/index"/>
</menuItem>
</children>
</menuItem>
In this example, we’ve added a new menu item called Custom Tab as a child of the Sales tab. We’ve also created another menu item called New Page, which is linked to an action in our module.
- Create the New Page
Now you need to create a controller and view for your new page:
- Create a new file at
app/code/Vendor/Module/Controller/Adminhtml/NewPage.php. - Implement the
executemethod to handle your business logic. - Create the corresponding view files in
app/code/Vendor/Module/view/adminhtml/templates/newpage/index.phtml.
Conclusion
Adding a custom menu item to Magento 2’s Sales sidebar involves editing XML files and creating new controllers and views. This guide demonstrates how to achieve this using a simple example.
Remember, when working with Magento 2, it’s crucial to follow the module structure and use the right XML files for each purpose. This ensures code separation and makes your customizations easier to manage.
For more advanced customizations and best practices in Magento 2 development, check out our guide on Magento 2 Module Development at Laravel Company.
Happy coding!