【问题标题】:OpenCart 3 common/header variable in extension扩展中的 OpenCart 3 通用/标题变量
【发布时间】:2019-07-01 03:26:40
【问题描述】:

我想在 common/header twig 文件中添加一个变量/显示,可以通过新的扩展进行管理。新扩展已创建。启动模块

我添加了: admin/view/template/extension/module/starter_module.twig

<div class="form-group">
        <label class="col-sm-2 control-label" for="input-new">New</label>
        <div class="col-sm-10">
        <select name="new" id="input-new" class="form-control">
            {% if new %}
            <option value="1" selected="selected">Enabled</option>
            <option value="0">Disabled</option>
            {% else %}
            <option value="1">Enabled</option>
            <option value="0" selected="selected">Disabled</option>
            {% endif %}
        </select>
        </div>
</div>

在 admin/controller/extension/module/starter_module.php 中

if (isset($this->request->post['new'])) {
  $data['new'] = $this->request->post['new'];
} elseif (!empty($module_info)) {
  $data['new'] = $module_info['new'];
} else {
  $data['new'] = '';
}

在目录/控制器/扩展/模块/starter_module.php中

            $data['new'] = $this->config->get('new');

            $data['new'] = (int) $setting['new'];  

在目录/视图/主题/默认/模板/common/header.twig中

{% if new %}Enabled {% else %} disabled{% endif %}

但我总是只得到禁用的结果,缺少什么?不能将变量从扩展发送到公共标头?

如果您知道问题,请帮助我,非工作文件在这里https://github.com/bblori/Opencart3

您可以在这里看到我在设置/设置文件中设置的工作变量之一并且正在工作。

https://github.com/bblori/Enable-Style-OC3

XML 代码

<modification>
<name>Starter Module</name>
<code>starter-module</code>
<version>1.0.0</version>
<author>Author</author>
<link>http://domain/</link>
    <file path="catalog/controller/common/header.php">
        <operation>
            <search><![CDATA[return $this->load->view('common/header', $data);]]></search>
            <add position="before"><![CDATA[
                    $data['config_new'] = $this->config->get('config_new'); 
           ]]></add>
        </operation>
    </file>

非常感谢

【问题讨论】:

  • 解决方案:我在 vqmod 中创建了一个 xml 文件,在其中添加了要插入到控制器/标题中的代码。

标签: opencart opencart-3


【解决方案1】:

将您的代码从您的入门模块移动到 header.php

$data['new'] = $this->config->get('new');
$data['new'] = (int) $setting['new']; 

【讨论】:

  • 谢谢,但这不是解决方案,万一更新,就会消失。
  • 在 header 模板中发送值,需要在 header 控制器中设置值,或者如果要设置全局值,则需要创建注册表
  • 谢谢 Trinkal,如果您有空闲时间,请查看此代码github.com/bblori/OpenCart3-Module-Header-Image 我无法保存图像。
【解决方案2】:

编辑核心文件或使用vqmod 都不可接受。不应修改核心文件,因为以后的更新会使您的修改过时。另一方面,Vqmod 为精心设计的系统增加了不必要的复杂性。

自第 3 版以来,Opencart 团队推出了活动。事件是在需要时执行自定义功能的一种新方式。下次遇到类似问题时添加事件(手动或在安装模块期间,如下所示)。

public function install() {
    $this->load->model('setting/event');
    $this->model_setting_event->addEvent('my_data_manager', 'catalog/view/*/before', 'extension/module/my_data_manager/beforeAll');
}

在执行周期的后期,每次呈现 common/header 时都会自动调用您的函数。

class ControllerExtensionModuleMyDataManager extends Controller {

    public function beforeAll(&$route, &$data, &$output){
        if ($route == 'common/header') {
            $data['my_custom_data'] = 'Mickey Mouse is not a bird!';
        }
    }
}

最后将{{ my_custom_data }}添加到template/common/header.twig

【讨论】:

  • 感谢您的帮助,请您指定应在哪个文件中插入哪个代码,我是编码的初学者。
猜你喜欢
  • 1970-01-01
  • 2018-06-27
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多