Add submenu in OpenCart by events in admin area

1.8k Views Asked by At

I want to add a sub menu in OpenCart, under catalog menu in admin area. in past we used ocmod or vqmod for do this, an example by ocmod is here:

<?xml version="1.0" encoding="utf-8"?>
<modification>
    <code>submenu5</code>
    <name>submenu5</name>
    <version>2.3</version>
    <author>codertj</author>
    <link>codertj.com</link>

    <!-- edit header controller -->
    <file path="admin/controller/common/column_left.php">
    <!-- create link to your page -->   
        <operation error="log">
            <search><![CDATA[if ($this->user->hasPermission('access', 'catalog/product')) {]]></search>
            <add  position="before"><![CDATA[
                if ($this->user->hasPermission('access', 'catalog/product')) {
                    $catalog[] = array(
                        'name'     => $this->language->get('text_hello_world'),
                        'href'     => $this->url->link('report/helloworld', 'token=' . $this->session->data['token'], true),
                        'children' => array()   
                    );
                }
            ]]></add>
        </operation>
    </file>

    <!-- edit header template -->
    <file path="admin/language/en-gb/common/column_left.php">
        <operation error="log">
            <search><![CDATA[$_['text_product']]]></search>
            <add  position="before"><![CDATA[
               $_['text_hello_world']             = 'Hello World';
            ]]></add>
        </operation>
    </file>

</modification> 

Now opencart use of Events system, but I can't find solution for convert this ocmod to event!

2

There are 2 best solutions below

2
On BEST ANSWER

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:

INSERT INTO `oc_event` (`code`, `trigger`, `action`, `status`) VALUES ('mymodule', 'admin/view/common/column_left/before', 'extension/module/mymodule/addSubmenu', 1)

admin\controller\extension\module\mymodule.php

<?php
class ControllerExtensionModuleMymodule extends Controller {
    public function addSubmenu(&$route = false, &$data = false, &$output = false){
        $my_language = $this->load->language('extension/module/mymodule');
        $data['menus'][1]['children'][] = array(
            'name'     => $my_language['text_hello_world'],
            'href'     => $this->url->link('report/helloworld', 'token=' . $this->session->data['token'], true),
            'children' => array()
        );
    }
}

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

<?php
$_['text_hello_world']      = 'Hello World!';

I tested this with OpenCart 2.3

0
On

How to add admin menu entry for Opencart 3x under a given admin menu item using events

This current topic is about injecting a submenu item above the Catalog -> Products link

  1. Delete the event if it exists, register the event (in your extension on install maybe)

Option A: use Opencart's method

$this->load->model('setting/event');

$this->model_setting_event->deleteEvent('MY_EVENT');

$this->model_setting_event->addEvent('MY_EVENT', 'admin/view/common/column_left/before', 'extension/module/MY_EXTENSION/ADDTOADMINMENU');

Option B: inject your code in the database from an Opencart model (or even from a controller function if you don't care that much about MVC):

    $this->db->query("
        INSERT INTO
            `oc_event`
            (`code`, `trigger`, `action`, `status`, `sort_order`)
            VALUES
            ('MY_EVENT', 'admin/view/common/column_left/before', 'extension/module/MY_EXTENSION/ADDTOADMINMENU', 1, 0)
    ");

Option C: run this query on the Opencart database (from phpMyAdmin, Adminer, etc.):

    INSERT INTO
        `oc_event`
        (`code`, `trigger`, `action`, `status`, `sort_order`)
        VALUES
        ('MY_EVENT', 'admin/view/common/column_left/before', 'extension/module/MY_EXTENSION/ADDTOADMINMENU', 1, 0)
  1. Add the event public function to your extension

    public function ADDTOADMINMENU(&$route, &$data){
    
        /**
        * Check if current logged in user has permission to access that link
        * Replace "extension/module/MY_EXTENSION" with your target path
        * This check can very well be ignored/deleted...
        **/
    
        if ($this->user->hasPermission('access', 'extension/module/MY_EXTENSION')) {
            $my_menu_entry = array(
                'id'       => 'menu-MY_EXTENSION',
                'icon'     => 'fa-check',
                'name'     => 'My menu entry',
                'href'     => $this->url->link('extension/module/MY_EXTENSION', 'user_token=' . $this->session->data['user_token'], true),
                'children' => array()
            );
    
            $target_menu_id      = 'menu-catalog';
            $target_submenu_href = $this->url->link('catalog/product', 'user_token=' . $this->session->data['user_token'], true);
    
            $new_menu = array();
    
            foreach( $data['menus'] as &$menu ) {
    
                if( $menu['id'] == $target_menu_id ) {
    
                    $new_submenu = array();
    
                    foreach( $menu['children'] as $submenu ) {
                        if( $submenu['href'] == $target_submenu_href ) {
                            $new_submenu[] = $my_menu_entry;
                            $new_submenu[] = $submenu;
                        } else {
                            $new_submenu[] = $submenu;
                        }
                    }
                    $menu['children'] = $new_submenu;
                    $new_menu[] = $menu;
                } else {
                    $new_menu[] = $menu;
                }
            }
    
            $data['menus'] = $new_menu;
        }
    }