Post by chriso » Mon Sep 15, 2014 11:52 pm

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?

Thanks

Newbie

Posts

Joined
Wed Sep 10, 2014 3:19 am

Post by Qphoria » Tue Sep 16, 2014 1:12 am

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:
http://forum.opencart.com/viewtopic.php?f=20&t=4113
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.

Image
Donate!|OpenCart Basics|GeoZones
Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by chriso » Thu Sep 18, 2014 12:00 am

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

<?php
// Configuration
if (file_exists('config.php')) {
	require_once('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
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_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:
		case E_USER_NOTICE:
			$error = 'Notice';
			break;
		case E_WARNING:
		case E_USER_WARNING:
			$error = 'Warning';
			break;
		case E_ERROR:
		case E_USER_ERROR:
			$error = 'Fatal Error';
			break;
		default:
			$error = 'Unknown';
			break;
	}
		
	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
set_error_handler('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']);
$language->load($languages[$config->get('config_admin_language')]['filename']);	
$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 />';
				}

Newbie

Posts

Joined
Wed Sep 10, 2014 3:19 am
Who is online

Users browsing this forum: No registered users and 52 guests