0

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!

DigitCart
  • 2,980
  • 2
  • 18
  • 28
Hamid Abbasi
  • 346
  • 2
  • 14
  • You can still do this with vQmod. That would be the best and most straight forward way to go. – Scott C Wilson Jul 24 '17 at 11:12
  • Thanks Scott, i know this and did that, but i want to learn how do that by events, this is learning aspect for me... – Hamid Abbasi Jul 24 '17 at 11:14
  • OpenCart events are *completely* different in versions 2.0 and 2.3. Then they're different again in version 3.0. What version are you wanting to learn? – Scott C Wilson Jul 24 '17 at 11:32
  • version 2.3.X and version 3 – Hamid Abbasi Jul 24 '17 at 11:46
  • I also don't think you'll be able to change the UI with events. By the time the event is triggered, the view has either not been rendered yet or has been completely rendered. But you can try it in 2.3 - see http://www.yellopen.com/opencart-2.3.x-event-system – Scott C Wilson Jul 24 '17 at 12:20
  • I do not think it will come to a results... , Unfortunately, OpenCart's documents are incomplete and all explanations are abandoned and defective – Hamid Abbasi Jul 26 '17 at 06:23
  • @HamidAbbasi سوالی در مورد سیستم رویداد دارید، تا جایی که بتونم راهنمایی می کنم. – DigitCart Jan 02 '18 at 11:30

2 Answers2

2

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

DigitCart
  • 2,980
  • 2
  • 18
  • 28
1

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;
        }
    }
    
George-Paul B.
  • 544
  • 4
  • 7