1

Is it possible in opencart 2.0 to get the value from the settings. For example as it was in previous opencart versions I could store value to database via settings, thus I did not need any model file to write to database and then from anywhere I could get the value by:

$this->config->get('key_that_you_need');

... Now this still works but only when in controller. What I need is to get the value even when I'm in template file (*.tpl)

When I want to use this in *.tpl file I get this error:

Notice: Undefined property: Loader::$config

same situation applies for permissions... now you can't use this example from template:

<?php if($this->user->hasPermission('access','catalog/attribute')) { ?>

...however it is still functional in controller file

Does anyone know if it is possible or some workaround ?

Naruko17
  • 47
  • 1
  • 7

1 Answers1

5

You shouldn't be using it in your template in the first place - You should be setting the data for the in your controller and passing that to the template. However You can do this pretty easily. Firstly, you need to bring the $config variable into the template and then call it directly, not use $this

<?php
global $config;
$somevar = $config->get('key_that_you_need');
?>

Not tested but should still work just fine

Jay Gilford
  • 15,141
  • 5
  • 37
  • 56
  • Thank you Jay this worked for the $config thing like a charm. I will try to implement it through controller though as you mentioned, however how it is with the permissions ? I tried the same approach as for config but without luck. Can I call user in template and in template ask/check if the user has the permission ? (if user > has permission(catalog/articles) > do something ?) – Naruko17 Oct 03 '14 at 11:42
  • The "User" object is admin side only. Is that what you mean? – Jay Gilford Oct 03 '14 at 12:25
  • Yes, only admin side. – Naruko17 Oct 03 '14 at 12:58
  • `global $registry; $user = $registry->get('user');` – Jay Gilford Oct 03 '14 at 17:57
  • @JayGilford how to set in latest OpenCart 2.3.0.2? please check this question. http://stackoverflow.com/questions/38830286/how-to-setting-global-variables-in-opencart-2-3-0-2 – HDP Aug 09 '16 at 05:32