Call us Today 877.446.9932
QuestionsCategory: OpencartHow to edit opencart admin side bar?
Marty Staff asked 2 months ago

How can we edit the menu in the admin?

1 Answers
admin Staff answered 2 months ago

Normally the admin sidebar is not editable but don’t let that stop you. Maybe your adding an admin feature and you want to add a new link or even a new admin section.n So there is an easy way and a hard way and an even harder way. The easy way is to do it using a plugin like this:

https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=6916

or this:

https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=21935

If your a newbie I would recommend that method.

The next way which is somewhat hard for a non coder is via an event:

You can do that in this way, We assume that you have recorded the event in the database, if you did not do this, you can create it quickly with the following query:

First create the event in the database.

INSERT INTO `oc_event` (`code`, `trigger`, `action`, `status`) VALUES ('yourmodulename', 

'admin/view/common/column_left/before', 'extension/module/mymodule/addYourSubmenuName', 1)

admin\controller\extension\module\mymodule.php

Then create the code for the event:

<?php

class ControllerExtensionModuleYourmodulename extends Controller {

public function addYourSubmenuName(&$route = false, &$data = false, &$output = false){

$my_language = $this->load->language('extension/module/yourmodulename');

$data['menus'][1]['children'][] = array(

'name' => $my_language['text_hello_world'],

'href' => $this->url->link('report/yourlink', 'token=' . $this->session->data['token'], true),

'children' => array()

);

}

}

Place this in:

admin\language\en-gb\extension\module\yourmodulename.php

<?php

$_['text_hello_world'] = 'Hello World!';

If you want to get your hands dirty and code it the classic way (even if just to learn) then the first thing you need to understand about Opencart is that it has whats called an MVC architecture. This means Model, View Controller. It is a way of seperating different types of code in an application. You can read about that on on our answer about What is MVC.

Now the next Thing you need to know is how to use OCMOD or EVENTS because editing core Opencart files is a really bad thing to do. This is because it makes upgrading your Opencart a big problem and maintaint your custom code even more of a problem. So if you don’t know anything about OCMOD (or even VQMOD its oder version) then read about those first.

So now with that out of the way lets get nitty gritty:

The code for OpenCart version 3 is below to add a menu but we are adding it where we want it which I will go over next:

<?xml version="1.0" encoding="utf-8"?>

<modification>

<name>Add Menu Item In Left Navigation Of Admin Menu</name>

<version>3.0</version>

<author>Weismann Web</author>

<link>https://weismannweb.com</link>

<code>weismannweb_add_menu_item_in_left_navigation_of_admin_menu</code>

<description>Add Menu Item In Left Navigation Of Admin Menu</description>

<file path="admin/controller/common/column_left.php">

<operation>

<search><![CDATA[ if ($this->user->hasPermission('access', 'marketplace/extension')) { ]]></search>

<add position="after"><![CDATA[

$marketplace[] = array(

'name' => "Your Menu Name",

'href' => $this->url->link('marketplace/extension&type=module', 'user_token=' . $this->session->data['user_token'], true),

'children' => array()

);

]]></add>

</operation>

</file>

</modification>

The code for OpenCart version 2 is below:

<?xml version="1.0" encoding="utf-8"?>

<modification>

<name>Add Menu Item In Left Navigation Of Admin Menu</name>

<version>2.0</version>

<author>Weismann Web</author>

<link>https://weismannweb.com</link>

<code>weismannweb_add_menu_item_in_left_navigation_of_admin_menu</code>

<description>Add Menu Item In Left Navigation Of Admin Menu</description>

<file path="admin/controller/common/column_left.php">

<operation>

<search><![CDATA[ if ($this->user->hasPermission('access', 'extension/extension')) { ]]></search>

<add position="after"><![CDATA[

$extension[] = array(

'name' => "Your Menu Name",

'href' => $this->url->link('extension/extension&type=module', 'token=' . $this->session->data['token'], true),

'children' => array()

);

]]></add>

</operation>

</file>

</modification>

So basically both of those ocmod versions are finding a string in the specific Opencart files and adding code after those strings. Think of this like a find and replace. It allows you to make the change without editing the core file.

The important part you need to know to make this work for you (considering you probably want to add your menu somewhere else) is this:

In v3:

<search><![CDATA[ if ($this->user->hasPermission('access', 'marketplace/extension')) { ]]></search>

In V2:

<search><![CDATA[ if ($this->user->hasPermission('access', 'extension/extension')) { ]]>

This is the string the find and replace is searching for.

The other important part is what file is being modified. You can see that here:

<file path="admin/controller/common/column_left.php">

So now your job is to look at that file and determine where you want your new menu to be. I assume you now the basics of PHP to read it well enough to know which menu itesm and sub menu item are what.

Now the third important thing is what is the code being added after that searched code? That woudl be this:

<add position="after"><![CDATA[

$marketplace[] = array(

'name' => "Your Menu Name",

'href' => $this->url->link('marketplace/extension&type=module', 'user_token=' . $this->session->data['user_token'], true),

'children' => array()

);

]]></add>

Now if you look at that file I mentioned (go to the path in your websites FTP from the root of the site.) you will see lots of blocks like that. This is because each one of those blocks of code is adding a menu item.

So in the end this OCmod is adding a menu item in the extensions Marketplace menu.