OpenCart: How to make a global variable?

12,083

Solution 1

you can use $GLOBALS super global array

for example declare it first in controller/common/header.php

$GLOBALS["1"] = "test";

then use it in any tpl file like

<?php echo $GLOBALS["1"]; ?>

regarding that header thing, that header and five other files are actually declared in every controller file (corresponding to every tpl file ) like this

$this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'     
        );

Answer to updated question

/catalog/controller/common/header.php

<?php   
class ControllerCommonHeader extends Controller {


    protected function index() {

        // NEW GLOBAL VARS
     $GLOBALS["cdnDefault"]="//www.gorgeouscouturedev.com/catalog/view/theme/";
     $GLOBALS["currentUseLang"] = $this->language->get('code');

And now in /catalog/view/theme/default/template/common/home.tpl

<?=$header?>
<?=$column_left?>
<?=$column_right?>
<div id="content">

<? echo $GLOBALS["cdnDefault"]; ?>
<? echo $GLOBALS["currentUseLang"]; ?>

    <?=$content_top?>
        <div class="flexslider">
            <ul class="slides">
                <li><img src="catalog/view/theme/default/image/desktop.png"/></li>
                <li><img src="catalog/view/theme/default/image/blogger.png"/></li>
            </ul>
        </div>
    <?=$content_bottom?>
</div>
<?=$footer?>

Solution 2

If you are just wanting to use a static value, you can just use a constant. Simply create one in your config.php file(s) such as

define('CDN_URL', 'http://cdn.someurl.com/');

You can then use

<?php echo CDN_URL; ?>

anywhere in your application. If you want to code it like you have in your edited question, th fundamental flaw with your code is that you are using $cdnDefault instead of $this->data['cdnDefault'] in your controller file, causing the undefined issue. Note however that this variable is not global in the slightest, it's merely been coded as it should be

In my opinion, the right way to do this would be to create a new setting value for you to edit in SYSTEM > SETTINGS in your administration area, and then call it using $this->config->get('config_value_here') rather than take what is considered more of a quick hack method

Share:
12,083
TheBlackBenzKid
Author by

TheBlackBenzKid

Ecommerce specialist with 15+ years of experience in development, design, marketing and management. Application Acceleration CDN, Load balancing and performance NodeJS, Vanilla JS, AngularJS IBM WebSphere Commerce Fast Fashion advocate - love ecommerce, love luxury fashion Oracle ATG Web Commerce OPENCART Stack: NodeJS, PHP, NGINX, Smarty or a JS view framework Omni channel and mutli channel advocate PHP/MySQL/HTML5/CSS3/JavaScript/Nginx/Rackspace/AWS/jQuery/SaSS also GULP over WebPack (some fanboy love for ya!) oh.. and DEFO FLASH and GIF! As Gif and Flash will always come back with a boom! 2018 Big fan of Blockchain, Cryptography, Hash graph and block chain development

Updated on July 22, 2022

Comments

  • TheBlackBenzKid
    TheBlackBenzKid almost 2 years

    I am trying to make two global variables within OpenCart. I basically want to be able to declare them in any of my .tpl files

    <?php echo $global1; ?>
    

    I have tried editing, library/system.php and also config.php by adding $global1="test" inside my files. However calling that in .tpl files is not working?

    Example, look at the file below, I want to be able to call these variables anytime.. do I have to edit config.php or what?? The example shows the $header call which is used on every .tpl file.

    not_found.tpl

    <?=$header?>
    <div class="breadcrumb">
    <? foreach ($breadcrumbs as $breadcrumb) { ?>
        <? $breadcrumb['separator']; ?><a href="<?=$breadcrumb['href']?>"><?=$breadcrumb['text']?></a>
    <? } ?>
    </div>
    <div id="content">
        <?=$global1?>
        <img src="/catalog/view/theme/default/image/error.png"/>
    </div>
    <?=$footer?>
    

    Updated

    /catalog/controller/common/header.php

    <?php   
    class ControllerCommonHeader extends Controller {
    
    
        protected function index() {
    
            // NEW GLOBAL VARS
            $cdnDefault="//www.gorgeouscouturedev.com/catalog/view/theme/";
            $currentUseLang = $this->language->get('code'); 
    

    And now in /catalog/view/theme/default/template/common/home.tpl

    <?=$header?>
    <?=$column_left?>
    <?=$column_right?>
    <div id="content">
    
    <? echo $cdnDefault ?>
    <? echo $currentUseLang ?>
    
        <?=$content_top?>
            <div class="flexslider">
                <ul class="slides">
                    <li><img src="catalog/view/theme/default/image/desktop.png"/></li>
                    <li><img src="catalog/view/theme/default/image/blogger.png"/></li>
                </ul>
            </div>
        <?=$content_bottom?>
    </div>
    <?=$footer?>
    

    And the errors:

     Notice: Undefined variable: cdnDefault in /catalog/view/theme/default/template/common/home.tpl on line 6
     Notice: Undefined variable: currentUseLang in /catalog/view/theme/default/template/common/home.tpl on line 7 
    
  • TheBlackBenzKid
    TheBlackBenzKid about 11 years
    Jay, thanks for the reply. I mentioned clearly I do not want to use sessions, it will always be a static variable, it is CDN URL, therefore not user driven. Can you tell me where I would put the session in anyway (for future use) and also your recommendation for the string instead?
  • TheBlackBenzKid
    TheBlackBenzKid about 11 years
    How would I add a new system setting value? You mean build a plugin for the admin panel to have then yes?
  • Jay Gilford
    Jay Gilford about 11 years
    Use vQmod to generate an extra field, and put the necessary controller code etc in the settings place. I'm not going to go into detail as to how exactly, it's something that's easy to source from the code itself and I'm sure there are examples on the web already