0

I'm new to PHP and magento. I'm trying to echo out a Custom Global Var called "my_code" in a .php file in the root to debug. None of the samples seem to work. Is there some reference I need to include?

<?php
require_once 'app/Mage.php';
//echo "1"; //This code works when uncomment

$value = Mage::getModel('core/variable')->setStoreId(Mage::app()->getStore()->getId())->loadByCode('my_code')->getValue('text');

echo $value; //This doesn't
1
  • What store id does echo Mage::app()->getStore()->getId() return and is the variable set for that store id? Commented Feb 9, 2016 at 21:13

2 Answers 2

0

Requiring the Mage.php file is not enough to bootstrap Magento as the application still isn't initialised, so none of the following statements will work.

At the bare minimum you are going to want something along the lines of:

require_once 'app/Mage.php';

// Run the engine as administrator
Mage::app('admin');

if( ! Mage::app()->isInstalled()) {
    die('Application is not installed.');
}

$value = Mage::getModel('core/variable')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByCode('my_code')
    ->getValue('text');
1
  • This seems to have worked with the "run as admin"
    – user123976
    Commented Feb 10, 2016 at 0:20
0

to be sure that Magento is bootstrapped completely, you should start by creating a Magento shell script to try things like that.

Create a file under /shell/ with the following content:

<?php
require_once 'abstract.php';
class My_Shell_Script extends Mage_Shell_Abstract
{
    public function run()
    {

    }

}
$shell = new My_Shell_Script();
$shell->run();

You can add your logic in the run method.

I'm not sure what you are exactly trying to do.

===UPDATE===

This does actually work (With Store ID 0):

<?php
require_once 'abstract.php';
class My_Shell_Script extends Mage_Shell_Abstract
{
    public function run()
    {
        Mage::getModel('core/variable')->setCode('my_code')->setPlainValue('testTextValue')->save();
        echo Mage::getModel('core/variable')->loadByCode('my_code')->getValue('text');
    }

}
$shell = new My_Shell_Script();
$shell->run();

are you sure your variable is saved correctly in your database? You might want to look it up in the tables core_variable and core_variable_value

2
  • "I'm not sure what you are exactly trying to do." Just echo a custom var.
    – user123976
    Commented Feb 10, 2016 at 0:11
  • ah sry, was a little blind yesterday .. Commented Feb 10, 2016 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.