将类别导入到opencart的脚本不起作用

时间:2019-01-18 10:46:19

标签: opencart

我有用于将类别和产品从XML导入Opencart的脚本。 该脚本是为Opencart 2.1.0.2编写的,现在我想将该脚本用于1.5.6.1版。

运行此脚本后,它给我写了一个错误-“加载XML失败:” 就是这样。

旧版本的Opencart的代码是否有所不同?

谢谢。

以下是用于导入类别的脚本示例:

<?php
// Version
define('VERSION', '1.5.6.1');

// Configuration
if (is_file('config.php')) {
    require_once('config.php');
}

// Install
if (!defined('DIR_APPLICATION')) {
    header('Location: install/index.php');
    exit;
}

// Startup
require_once(DIR_SYSTEM . 'startup.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, DB_PORT);
$registry->set('db', $db);
ob_start();

echo "<pre>";

// initialize xml
$xmlfiles = glob('./import/categories.xml');
if (count($xmlfiles) > 0) {
    $xmlfiletmp = $xmlfiles[0];
    $fileNameParts = explode('/', $xmlfiletmp);
    $xmlfile = end($fileNameParts);
} else {
    die('xml doesnt exist');
}

$xmlFileContent = file_get_contents('./import/categories.xml' . $xmlfile);

// $xmlFileContent = file_get_contents('./import/categories.xml');

$xml = simplexml_load_string($xmlFileContent);
if ($xml === false) {
    echo "Failed loading XML: ";
    foreach(libxml_get_errors() as $error) {
        echo "<br>", $error->message;
    }
    die();
} else {
    echo "XML loaded<br>";
}

$language_id = 4;
$store_id = 0;
$layout_id = 0;
$oldCategories = [];
$newCategories = [];


// load old categories
$query = $db->query("SELECT * FROM `" . DB_PREFIX . "category`");
foreach ($query->rows as $result) {
    $oldCategories[$result['menu_id_abra']] = $result;
}

// print_r($oldCategories['9000000101']);
// die('s');

echo "###### START<br>";
// loop new categories
foreach ($xml as $category) {
    // $newCategories[$category['Menu_id_abra']] = $category;
    // $oldCategories[$category['Menu_id_abra']] = $category;

    $cId = (string)$category['Menu_id_abra'];
    $parentIdAbraCasted = (string)$category['Parent_id_abra'];

    // parent exists?
    $parentId = 0;
    if (isset($oldCategories[$parentIdAbraCasted])) {
        $parentId = $oldCategories[$parentIdAbraCasted]['category_id'];
    }

    $status = (string)$category['aktivne'] == 'Áno' ? 0 : 1;

    if (isset($oldCategories[$cId])) {
        // update
        echo "Updating ... ";

        $category_id = $oldCategories[$cId]['category_id'];

        $db->query("UPDATE " . DB_PREFIX . "category SET parent_id = '" . (int)$parentId . "', `top` = '1', `column` = '1', status = '" . $status . "', date_modified = NOW(), menu_id_abra = '" . $category['Menu_id_abra'] . "', parent_id_abra = '" . $category['Parent_id_abra'] . "', pos_index_abra = '" . $category['PosIndex'] . "', sort_order = '" . $category['PosIndex'] . "' WHERE category_id = '" . (int)$category_id . "'");

        $db->query("UPDATE " . DB_PREFIX . "category_description SET name = '" . $db->escape($category['Nazov_menu']) . "', meta_title = '" . $db->escape($category['Nazov_menu']) . "' WHERE category_id = '" . (int)$category_id . "' AND language_id = '" . (int)$language_id . "'");


        $db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'category_id=" . (int)$category_id . "'");
        $db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'category_id=" . (int)$category_id . "', keyword = '" . $db->escape(toURI($category['Nazov_menu'])) . "'");

        $db->query("UPDATE " . DB_PREFIX . "category_to_layout SET store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout_id . "' WHERE category_id = '" . (int)$category_id . "'");


        // MySQL Hierarchical Data Closure Table Pattern
        $db->query("DELETE FROM " . DB_PREFIX . "category_path WHERE category_id = '" . (int)$category_id . "'");
        $level = 0;
        $query = $db->query("SELECT * FROM `" . DB_PREFIX . "category_path` WHERE category_id = '" . (int)$parentId . "' ORDER BY `level` ASC");
        foreach ($query->rows as $result) {
            $db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET `category_id` = '" . (int)$category_id . "', `path_id` = '" . (int)$result['path_id'] . "', `level` = '" . (int)$level . "'");
            $level++;
        }
        $db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET `category_id` = '" . (int)$category_id . "', `path_id` = '" . (int)$category_id . "', `level` = '" . (int)$level . "'");


        echo 'updated with ID ' . $category_id;

    } else {
        // create
        echo "Creating ... ";

        $db->query("INSERT INTO " . DB_PREFIX . "category SET parent_id = '" . (int)$parentId . "', `top` = '0', `column` = '1', status = '" . $status . "', date_modified = NOW(), date_added = NOW(), menu_id_abra = '" . $category['Menu_id_abra'] . "', parent_id_abra = '" . $category['Parent_id_abra'] . "', pos_index_abra = '" . $category['PosIndex'] . "', sort_order = '" . $category['PosIndex'] . "'");

        $category_id = $db->getLastId();

        $db->query("INSERT INTO " . DB_PREFIX . "category_description SET category_id = '" . (int)$category_id . "', language_id = '" . (int)$language_id . "', name = '" . $db->escape($category['Nazov_menu']) . "', description = '" . $db->escape($category['Nazov_menu']) . "', meta_title = '" . $db->escape($category['Nazov_menu']) . "'");

        $db->query("INSERT INTO " . DB_PREFIX . "category_to_store SET category_id = '" . (int)$category_id . "', store_id = '" . (int)$store_id . "'");

        $db->query("INSERT INTO " . DB_PREFIX . "category_to_layout SET category_id = '" . (int)$category_id . "', store_id = '" . (int)$store_id . "', layout_id = '" . (int)$layout_id . "'");

        $db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'category_id=" . (int)$category_id . "', keyword = '" . $db->escape(toURI($category['Nazov_menu'])) . "'");

        // MySQL Hierarchical Data Closure Table Pattern
        $level = 0;
        $query = $db->query("SELECT * FROM `" . DB_PREFIX . "category_path` WHERE category_id = '" . (int)$parentId . "' ORDER BY `level` ASC");
        foreach ($query->rows as $result) {
            $db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET `category_id` = '" . (int)$category_id . "', `path_id` = '" . (int)$result['path_id'] . "', `level` = '" . (int)$level . "'");
            $level++;
        }
        $db->query("INSERT INTO `" . DB_PREFIX . "category_path` SET `category_id` = '" . (int)$category_id . "', `path_id` = '" . (int)$category_id . "', `level` = '" . (int)$level . "'");

        // $newCategories[$category['Menu_id_abra']]['category_id'] = $category_id;
        $oldCategories[$cId] = ['category_id'=>$category_id];

        echo 'created with ID ' . $category_id;
    }
    echo '<br>';
}

// print_r($oldCategories);
rename ("./import/" . $xmlfile, "./import/IMPORTED/" . $xmlfile);

$htmlStr = ob_get_contents();
// Clean (erase) the output buffer and turn off output buffering
ob_end_clean();
file_put_contents('./importlogs/categoriesimport_' . time() , $htmlStr);
echo $htmlStr;

function replace_accent($str)
{
  $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'IJ', 'ij', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ʼn', 'Ō', 'ō',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n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
  return str_replace($a, $b, $str);
}
function toURI($str, $replace = array(), $delimiter = '-')
{
    if(!empty($replace))
    {
        $str = str_replace((array) $replace, ' ', $str);
    }

    $clean=replace_accent($str);
    $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
    $clean = strtolower(trim($clean, '-'));
    $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

    return $clean;
}

0 个答案:

没有答案