OpenCart 3 custom module doesn't save data

☆樱花仙子☆ 提交于 2019-12-25 12:05:04

问题


Im developing a payment module for OpenCart 3. Since there's no updater documentation my module is based on the others payment module (such as Alipay, paypal, cash on demand, etc....)

I've created the module view on admin/view/template/extension/payment/mipago.twig as simply

{{ header }}{{ column_left }}
{% if error_warning %}
    {{ error_warning }}
{% endif %}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
        <button type="submit" form="form-mipago" data-toggle="tooltip" title={{ button_save }} class="btn btn-primary"><i class="fa fa-save"></i></button>
        <a href={{ cancel }} data-toggle="tooltip" title={{ button_cancel }} class="btn btn-default"><i class="fa fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ul class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
            <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ul>
    </div>
  </div>
  <div class="container-fluid">
    {% if error_warning %}
    <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i>{{ error_warning }}
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    {% endif %}
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-pencil"></i>{{ text_edit }}</h3>
      </div>
      <div class="panel-body">
        <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-mipago" class="form-horizontal">


               <div class="form-group">
                    <label class="col-sm-2 control-label" for="input-status">{{ entry_status }}</label>
                    <div class="col-sm-10">
                        <select name="mipago_status" id="input-status" class="form-control">
                            {% if paygol_status %}
                            <option value="1" selected="selected">{{ text_enabled }}</option>
                            <option value="0">{{ text_disabled }}</option>
                            {% else %}
                            <option value="1">{{ text_enabled}}</option>
                            <option value="0" selected="selected">{{ text_disabled }}</option>
                            {% endif %}
                        </select>
                    </div>
              </div>
        </form>
      </div>
    </div>
  </div>
</div>
{{ footer }}

And the controller admin/controller/extension/payment/mipago.php

<?php
class ControllerExtensionPaymentMiPago extends Controller {
    private $error = array();

    public function index() {
        $this->document->setTitle('Mi Pago');

        $this->load->model('setting/setting');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->model_setting_setting->editSetting('payment_mipago', $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true));
        }

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = "'';
        }

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

        $data['breadcrumbs'][] = array(
            'text' => 'Inicio',
            'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
        );

        $data['breadcrumbs'][] = array(
            'text' => 'Extensiones',
            'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)
        );

        $data['breadcrumbs'][] = array(
            'text' => 'Mi Pago',
            'href' => $this->url->link('extension/payment/mipago', 'user_token=' . $this->session->data['user_token'], true)
        );

        $data['action'] = $this->url->link('extension/payment/mipago', 'user_token=' . $this->session->data['user_token'], true);

        $data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true);

        if (isset($this->request->post['mipago_status'])) {
            $data['mipago_status'] = $this->request->post['mipago_status'];
        } else {
            $data['mipago_status'] = $this->config->get('mipago_status');
        }

        if (isset($this->request->post['paygol_order_status_id'])) {
            $data['mipago_order_status_id'] = $this->request->post['mipago_order_status_id'];
        } else {
            $data['mipago_order_status_id'] = $this->config->get('mipago_order_status_id');
        }

        $this->load->model('localisation/order_status');
        $data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();

        if (isset($this->request->post['mipago_geo_zone_id'])) {
            $data['mipago_geo_zone_id'] = $this->request->post['mipago_geo_zone_id'];
        } else {
            $data['mipago_geo_zone_id'] = $this->config->get('mipago_geo_zone_id');
        }

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

        $this->response->setOutput($this->load->view('extension/payment/mipago', $data));
    }

    protected function validate() {
        if (!$this->user->hasPermission('modify', 'extension/payment/mipago')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }
        return !$this->error;
    }
}

This is a mockup but the problem its present, I cannot save the data from the form on the view. My code is based on other payment modules such as G2APay and Cash on Demand but even using the same lines of code (only changing the parameters) it doesnt save anything.


回答1:


Solved!

It was just a mismatch between the module name, it's type and all the variables and fields.

For example, my plugin needs to be a payment module, so all the variables should be be named payment_mipago_<field>, instead of mipago_<field>, including the fields that opencart inserts to the database




回答2:


For OC3.0 should be:

if (isset($this->request->post['payment_mipago_status'])) {
            $data['payment_mipago_status'] = $this->request->post['payment_mipago_status'];
        } else {
            $data['payment_mipago_status'] = $this->config->get('payment_mipago_status');
        }

and for all variables you must add payment



来源:https://stackoverflow.com/questions/47042257/opencart-3-custom-module-doesnt-save-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!