0

我想在 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 使用事件系统,但我找不到将此 ocmod 转换为事件的解决方案!

4

2 回答 2

2

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

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

管理员\控制器\扩展\模块\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 测试了这个

于 2018-01-02T11:29:38.630 回答
1

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

当前主题是关于在 Catalog -> Products 链接上方注入子菜单项

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

选项A:使用Opencart的方法

$this->load->model('设置/事件');

$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 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;
        }
    }
    
于 2019-09-11T23:21:27.370 回答