Opencart's $this->config->get('module_var_name')

16,919

In OpenCart administration a admin/model/setting/setting.php model is used for such a modules/extensions where only a key group name and the posted data is provided to be stored in the setting DB table... (and optionally a store_id in multistore installation as well)

You could check any of controllers in admin/controller/payment/ dir to see how is this model used (pp_standard.php, from line 10 within OC 1.5.5.1):

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

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

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

    $this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
}

This will write all the form data into the setting DB table while form field's name is used as a key to the value filled within.

Thus if You call

$this->config->get('<KEY>');

You can get the value that is set for the <KEY> key.

Share:
16,919
Ninjanoel
Author by

Ninjanoel

Updated on June 27, 2022

Comments

  • Ninjanoel
    Ninjanoel almost 2 years

    I'm trying to customize an Opencart payment module, I see many places where config info is being used, but I cant find anything that creates what variables are in use. I know in the admin pages if I select 'paypal standard' I can set all the 'config' info, but I cant find the 'model' underlining it, is there a model, I wish to create a new config setting, settable inside the admin page

    How does the admin page know which variables to set? If I change the admin 'view' for the payment module to show a new setting, will that setting automatically be available in the catalog?

    example of some of the config data in use...

    admin\view\template\payment\pp_standard.tpl (paypal admin template), allows 'test mode' to be set....

    <tr>
            <td><?php echo $entry_test; ?></td>
            <td><?php if ($pp_standard_test) { ?>
              <input type="radio" name="pp_standard_test" value="1" checked="checked" />
              <?php echo $text_yes; ?>
              <input type="radio" name="pp_standard_test" value="0" />
              <?php echo $text_no; ?>
              <?php } else { ?>
              <input type="radio" name="pp_standard_test" value="1" />
              <?php echo $text_yes; ?>
              <input type="radio" name="pp_standard_test" value="0" checked="checked" />
              <?php echo $text_no; ?>
              <?php } ?></td>
          </tr>
    

    catalog\controller\poayment\pp_standard.php (paypal catalog controller), uses above 'test mode' to determine which paypal Webservice URL to hit..

    if (!$this->config->get('pp_standard_test')) {
        $curl = curl_init('https://www.paypal.com/cgi-bin/webscr');
    } else {
        $curl = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
    }
    

    I was expecting to find a 'model' somewhere with 'pp_standard_test' defined somewhere, but I've found nothing, any help understanding this would be greatly appreciated.

    P.S. There is much advice online stating that the 'paypal standard' payment module is a good place to start, but more than likely, we wont be using paypal, it's the principle I'm trying to figure out.

  • Ninjanoel
    Ninjanoel almost 11 years
    Great, i've found the 'pp_standard' group in the <prefix>_setting table in mysql, and I can see everything getting stored in there. So essentially (as my php is a bit weak, all this MVC stuff doesn't help), any setting that I create on the admin page will automatically be inserted into the DB 'setting' table with <key><value>, the <key> being what I name the html input field? does it do something like 'all posted variables starting with 'pp_standard' in the name should be stored in the database? is that group set independently anywhere? and yeah, posted vars must all start with th group name?
  • shadyyx
    shadyyx almost 11 years
    The group name is just because You then can use setting model to get all the settings for this group. It should tie together all the settings for one module/extension. Yes, the name of the field is the <key> and value filled in it is the <value> then. And no, You do not need to name the field names starting with the group name, it is just for better recognition if You use $this->config->get('group_property'); on frontend to be wise the property is from the concrete group of settings...
  • Ninjanoel
    Ninjanoel almost 11 years
    i see $this->model_setting_setting->editSetting('pp_standard', $this->request->post);, so ALL variables it finds in post will be inserted into the db with the group 'pp_standard' in this instance? Thanks for the help @shadyyx
  • shadyyx
    shadyyx almost 11 years
    Exactly as You understood it. If You then call $this->model_setting_setting->getSetting('pp_standard'); You can on the other hand retrieve all the settings for this group name.
  • user3167249
    user3167249 over 9 years
    I am having a similar issue to what you are describing here. I have a setting in group um key alert_color and an setting in group um_redux key alert_color and $this->config->get('alert_color'); pulls in group um property