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

时间:2017-07-24 09:28:03

标签: opencart opencart2.x opencart2.3 ocmod opencart-events

我想在管理区域的目录菜单下的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转换为事件的解决方案!

2 个答案:

答案 0 :(得分: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()
        );
    }
}

管理\语言\ EN-GB \扩展\模块\ mymodule.php

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

我使用OpenCart 2.3进行了测试

答案 1 :(得分:0)

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

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

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