通过管理区域中的事件在 OpenCart 中添加子菜单

Add submenu in OpenCart by events in admin area

我想在 OpenCart 中添加一个子菜单,在管理区域的目录菜单下。 过去我们使用 ocmod 或 vqmod 来做到这一点, ocmod 的一个例子在这里:

<?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> 

现在opencart使用了Events系统,但是我找不到将这个ocmod转换为event的解决方案!

你可以这样做, 我们假设您已经将事件记录在数据库中,如果您没有这样做,您可以使用以下查询快速创建它:

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!';

我用 OpenCart 2.3 测试了这个

如何使用事件在给定的管理菜单项下添加 Opencart 3x 的管理菜单条目

当前主题是关于在目录 -> 产品上方插入子菜单项 link

  1. 删除事件(如果存在),注册事件(可能在您安装的扩展程序中)

选项A:使用Opencart的方法

$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');

选项 B:从 Opencart 模型(如果您不太关心 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)
    ");

选项 C:运行 在 Opencart 数据库上查询(来自 phpMyAdmin、Adminer 等):

    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. 将事件 public 功能添加到您的扩展程序

    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;
        }
    }