I am working on a script that grabs all the category and product data from my opencart webshop. Since i use a application to run this script i can't use the MVC structure of opencart, the php script has to be located at the rootfolder of opencart.
<?php
if (is_file('./admin/config.php')) {
require_once('./admin/config.php');
}
require_once(DIR_SYSTEM . 'startup.php');
//require_once(DIR_SYSTEM . 'library/cart/user.php');
$application_config = 'admin';
$registry = new Registry();
$loader = new Loader($registry);
//$loader->model('catalog/product');
$registry->set('load', $loader);
$config = new Config();
$config->load('default');
$config->load($application_config);
$registry->set('config', $config);
$registry->set('request', new Request());
$response = new Response();
$response->addHeader('Content-Type: text/html; charset=utf-8');
$registry->set('response', $response);
$registry->set('cache', new Cache($config->get('cache_type'), $config->get('cache_expire')));
$registry->set('url', new Url($config->get('site_ssl')));
$language = new Language($config->get('language_default'));
$language->load($config->get('language_default'));
$registry->set('language', $language);
$registry->set('document', new Document());
$event = new Event($registry);
$registry->set('event', $event);
if ($config->get('db_autostart')) {
$registry->set('db', new DB($config->get('db_type'), $config->get('db_hostname'), $config->get('db_username'), $config->get('db_password'), $config->get('db_database'), $config->get('db_port')));
}
if ($config->get('session_autostart')) {
$session = new Session();
$session->start();
$registry->set('session', $session);
}
if ($config->has('action_event')) {
foreach ($config->get('action_event') as $key => $value) {
$event->register($key, new Action($value));
}
}
if ($config->has('config_autoload')) {
foreach ($config->get('config_autoload') as $value) {
$loader->config($value);
}
}
if ($config->has('language_autoload')) {
foreach ($config->get('language_autoload') as $value) {
$loader->language($value);
}
}
if ($config->has('library_autoload')) {
foreach ($config->get('library_autoload') as $value) {
$loader->library($value);
}
}
if ($config->has('model_autoload')) {
foreach ($config->get('model_autoload') as $value) {
$loader->model($value);
}
}
class Myclass extends Controller
{
function login($usr, $pwd)
{
if ($this->user->login($usr, $pwd)) {
return TRUE;
} else {
echo('Error: Login failed.');
die;
}
}
function cleanPost($post)
{
$post = mb_convert_encoding($post, 'UTF-8', 'UTF-8');
$post = htmlentities($post, ENT_QUOTES, 'UTF-8');
return $post;
}
function getProductCount()
{
$this->model->load('catalog/product');
$products = $this->model_catalog_product->getProducts();
print_r($products);
}
}
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);
$registry->set('user', new Cart\User($registry));
$myAPI = new Myclass($registry);
if ($myAPI->login('user','password')) {
getProductCount()
}
?>
This codesnippet let's me login and use the product.php file located at: opencart\admin\controller\catalog. But when i run the code I get an empty array. So my question is: How do i include all the models/controllers in a custom made script in the rootfolder, or what am i doing wrong?
Thanks in advance!
EDIT: The code stated above works. Somehow the language ID wasn't set. This can be fixed by using:
$this->config->set("config_language_id",1);