Adding New Modules Positions to OpenCart 2

2020-05-06 17:14发布

I want to add more Positions for modules. How can I add a new module position in opencart 2?

Here, I like this information. but, it is work only for OpenCart older version. http://www.opencartnews.com/tutorials/adding-new-modules-positions/

How can the same thing be achieved in Opencart 2?

Please help...!

2条回答
Lonely孤独者°
2楼-- · 2020-05-06 17:45

I know it is bit late , but it might be helpful to others , also the above answer helped me but for newbie to understand easily i am writing this.

I am inserting the new position content_footer in it.

Tested For 2.3.0.2

We will edit the files for admin side first

1] Go to admin/view/template/design/layout_form.tpl

By Default opencart gives 4 position for modules find the below code

  <table id="module-content-bottom" class="table table-striped table-bordered table-hover">

so after copy this whole section from start of the table to the table closing and paste it below and change the content_bottom to content_footer

  <table id="module-content-footer" class="table table-striped table-bordered table-hover">
              <thead>
              <tr>
                <td class="text-center"><?php echo $text_content_footer; ?></td>
              </tr>
              </thead>
              <tbody>
              <?php foreach ($layout_modules as $layout_module) { ?>
              <?php if ($layout_module['position'] == 'content_footer') { ?>
              <tr id="module-row<?php echo $module_row; ?>">
                <td class="text-left"><div class="input-group">
                    <select name="layout_module[<?php echo $module_row; ?>][code]" class="form-control input-sm">
                      <?php foreach ($extensions as $extension) { ?>
                      <optgroup label="<?php echo $extension['name']; ?>">
                        <?php if (!$extension['module']) { ?>
                        <?php if ($extension['code'] == $layout_module['code']) { ?>
                        <option value="<?php echo $extension['code']; ?>" selected="selected"><?php echo $extension['name']; ?></option>
                        <?php } else { ?>
                        <option value="<?php echo $extension['code']; ?>"><?php echo $extension['name']; ?></option>
                        <?php } ?>
                        <?php } else { ?>
                        <?php foreach ($extension['module'] as $module) { ?>
                        <?php if ($module['code'] == $layout_module['code']) { ?>
                        <option value="<?php echo $module['code']; ?>" selected="selected"><?php echo $module['name']; ?></option>
                        <?php } else { ?>
                        <option value="<?php echo $module['code']; ?>"><?php echo $module['name']; ?></option>
                        <?php } ?>
                        <?php } ?>
                        <?php } ?>
                      </optgroup>
                      <?php } ?>
                    </select>
                    <input type="hidden" name="layout_module[<?php echo $module_row; ?>][position]" value="<?php echo $layout_module['position']; ?>" />
                    <input type="hidden" name="layout_module[<?php echo $module_row; ?>][sort_order]" value="<?php echo $layout_module['sort_order']; ?>" />
                    <div class="input-group-btn"><a href="<?php echo $layout_module['edit']; ?>" type="button" data-toggle="tooltip" title="<?php echo $button_edit; ?>" target="_blank" class="btn btn-primary btn-sm"><i class="fa fa-pencil"></i></a>
                      <button type="button" onclick="$('#module-row<?php echo $module_row; ?>').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger btn-sm"><i class="fa fa fa-minus-circle"></i></button>
                    </div>
                  </div></td>
              </tr>
              <?php $module_row++; ?>
              <?php } ?>
              <?php } ?>
              </tbody>
              <tfoot>
              <tr>
                <td class="text-left"><div class="input-group">
                    <select class="form-control input-sm">
                      <?php foreach ($extensions as $extension) { ?>
                      <optgroup label="<?php echo $extension['name']; ?>">
                        <?php if (!$extension['module']) { ?>
                        <option value="<?php echo $extension['code']; ?>"><?php echo $extension['name']; ?></option>
                        <?php } else { ?>
                        <?php foreach ($extension['module'] as $module) { ?>
                        <option value="<?php echo $module['code']; ?>"><?php echo $module['name']; ?></option>
                        <?php } ?>
                        <?php } ?>
                      </optgroup>
                      <?php } ?>
                    </select>
                    <div class="input-group-btn">
                      <button type="button" onclick="addModule('content-footer');" data-toggle="tooltip" title="<?php echo $button_module_add; ?>" class="btn btn-primary btn-sm"><i class="fa fa-plus-circle"></i></button>
                    </div>
                  </div></td>
              </tr>
              </tfoot>
            </table>

2] Now find the function addModule() and find the below line

$('#module-column-left, #module-column-right, #module-content-top, #module-content-bottom ').delegate

Now add #module-content-footer

$('#module-column-left, #module-column-right, #module-content-top, #module-content-bottom , #module-content-footer').delegate(

Same way below line for trigger

$('#module-column-left, #module-column-right, #module-content-top, #module-content-bottom , #module-content-footer' ).trigger

3] In admin/controller/design/layout.php find the below function

protected function getForm()

and this line in it

$data['text_content_footer'] = $this->language->get('text_content_footer');

4] Now add following line in admin/language/en-gb/layout.php

$_['text_content_footer'] = 'Content Footer';

Front Side Files Changing

1] Create a new template file in catalog/view/theme/themename/template/common/content_footer.tpl

<?php foreach ($modules as $module) { ?>

2] Create a new controller file in Catalog/controller/common/content_footer.php and place the below code

    <?php
class ControllerCommonContentFooter extends Controller {
public function index() {
    $this->load->model('design/layout');

    if (isset($this->request->get['route'])) {
        $route = (string)$this->request->get['route'];
    } else {
        $route = 'common/home';
    }

    $layout_id = 0;

    if ($route == 'product/category' && isset($this->request->get['path'])) {
        $this->load->model('catalog/category');

        $path = explode('_', (string)$this->request->get['path']);

        $layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));
    }

    if ($route == 'product/product' && isset($this->request->get['product_id'])) {
        $this->load->model('catalog/product');

        $layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
    }

    if ($route == 'information/information' && isset($this->request->get['information_id'])) {
        $this->load->model('catalog/information');

        $layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
    }

    if (!$layout_id) {
        $layout_id = $this->model_design_layout->getLayout($route);
    }

    if (!$layout_id) {
        $layout_id = $this->config->get('config_layout_id');
    }

    $this->load->model('extension/module');

    $data['modules'] = array();

    $modules = $this->model_design_layout->getLayoutModules($layout_id, 'content_footer');

    foreach ($modules as $module) {
        $part = explode('.', $module['code']);

        if (isset($part[0]) && $this->config->get($part[0] . '_status')) {
            $module_data = $this->load->controller('extension/module/' . $part[0]);

            if ($module_data) {
                $data['modules'][] = $module_data;
            }
        }

        if (isset($part[1])) {
            $setting_info = $this->model_extension_module->getModule($part[1]);

            if ($setting_info && $setting_info['status']) {
                $output = $this->load->controller('extension/module/' . $part[0], $setting_info);

                if ($output) {
                    $data['modules'][] = $output;
                }
            }
        }
    }

    return $this->load->view('common/content_footer', $data);
}}

3] Now add the following line in catalog/controller/common/home.php

$data['content_footer'] = $this->load->controller('common/content_footer');

4] Finally , to show the position on the home page, you'll add

<?php echo $content_footer; ?>

to catalog/view/theme/themename/template/common/home.tpl

enter image description here

查看更多
forever°为你锁心
3楼-- · 2020-05-06 17:57

You can achieve this by modifying the following core file.

  1. admin/view/template/design/layout_form.tpl add your new position in this select tag

    <select name="layout_module[<?php echo $module_row; ?>][position]"

    and also in this method at the bottom of the file

    function addModule() {

  2. And then you have to add a template and a controller in catalog/view/theme/default/template/common you can use the same code as it is in content_top.php & content_top.tpl, just rename the file as your new position name.

  3. And then in Home controller or any other page where this position is required, you have to add

    $data['new_position'] = $this->load->controller('common/new_position');

  4. After adding it to controller, you need to add it to the template files. Add to all the page templates you want to show the custom position on.

    <?php echo $new_position; ?>

For example, to show the position on the home page, you'll add <?php echo $new_position; ?> to catalog/view/theme/YOUR_THEME/template/common/home.tpl

查看更多
登录 后发表回答