我正在制作一个脚本,它可以从我的opencart网上商店获取所有类别和产品数据。由于我使用应用程序来运行此脚本,我无法使用opencart的MVC结构,因此php脚本必须位于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()
}
?>
这个codesnippet让我登录并使用位于以下位置的product.php文件:opencart \ admin \ controller \ catalog。但是当我运行代码时,我得到一个空数组。所以我的问题是:如何在rootfolder中的自定义脚本中包含所有模型/控制器,或者我做错了什么?
提前致谢!
编辑:上述代码有效。某种程度上没有设置语言ID。这可以通过使用:
来解决$this->config->set("config_language_id",1);