Page 1 of 1

Code testing in opencart 1.5

Posted: Mon Sep 15, 2014 11:52 pm
by chriso
Dear Opencart Community,

I have been working for a long time with oscommerce and when i wanted to test some code i could just create a new php-file and include a file called application_top.php and then i would have access to all classes/functions.

Is there a similar way to do this in Opencart or do i need to include most of the code like in the index.php file?


Re: Code testing in opencart 1.5

Posted: Tue Sep 16, 2014 1:12 am
by Qphoria
You can create controller files and it will include the "children" which are header, footer, and sidebars. But don't try to compare it to osCommerce or ZenCart's "application_top.php" method because it is different. Forget everything you know about those carts. Best place to start is here:
and then using that file breakdown, go through each file for a single context (like a payment module or the product page) and see how all the pieces call and link to eachother.

Re: Code testing in opencart 1.5

Posted: Thu Sep 18, 2014 12:00 am
by chriso
Thenks for your reply Qphoria,

I have already read your excellent guide for the structure.
I understand that I should forget everything from oscommerce since its a mixture of mostly procedural and a small bit of object orientated code.

But when i just want to test a piece of code or do some operations it was quite nice to have access to everything with a single include.
Because you might not want to create a model, view and a controller just for that.

Maybe I'm in the wrong direction but please see my example below, if I would just like to print out all the product names in the catalog.
Could this be done more simple and still have access to Opencarts functions? If not it's really not a problem since I can create my own includefile with this code except the sql.

Code: Select all

// Configuration
if (file_exists('config.php')) {

// Startup
require_once(DIR_SYSTEM . 'startup.php');

// Application Classes
require_once(DIR_SYSTEM . 'library/currency.php');
require_once(DIR_SYSTEM . 'library/user.php');
require_once(DIR_SYSTEM . 'library/weight.php');
require_once(DIR_SYSTEM . 'library/length.php');

// Registry
$registry = new Registry();

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

// Config
$config = new Config();
$registry->set('config', $config);

// Database
$registry->set('db', $db);

// Settings
$query = $db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE store_id = '0'");

foreach ($query->rows as $setting) {
	if (!$setting['serialized']) {
		$config->set($setting['key'], $setting['value']);
	} else {
		$config->set($setting['key'], unserialize($setting['value']));

// Log
$log = new Log($config->get('config_error_filename'));
$registry->set('log', $log);

function error_handler($errno, $errstr, $errfile, $errline) {
	global $log, $config;
	switch ($errno) {
		case E_NOTICE:
			$error = 'Notice';
		case E_WARNING:
			$error = 'Warning';
		case E_ERROR:
		case E_USER_ERROR:
			$error = 'Fatal Error';
			$error = 'Unknown';
	if ($config->get('config_error_display')) {
		echo '<b>' . $error . '</b>: ' . $errstr . ' in <b>' . $errfile . '</b> on line <b>' . $errline . '</b>';
	if ($config->get('config_error_log')) {
		$log->write('PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);

	return true;

// Error Handler

// Language
$languages = array();

$query = $db->query("SELECT * FROM `" . DB_PREFIX . "language`"); 

foreach ($query->rows as $result) {
	$languages[$result['code']] = $result;

$config->set('config_language_id', $languages[$config->get('config_admin_language')]['language_id']);

// Language	
$language = new Language($languages[$config->get('config_admin_language')]['directory']);
$registry->set('language', $language);

		$sql = "SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE pd.language_id = '" . (int)$config->get('config_language_id') . "' GROUP BY p.product_id";

				$results = $db->query($sql);
				foreach ($results->rows as $result) {
				 echo $result['name'].'<br />';