<?php require_once "core/config/connect.inc.php"; require_once "core/config/tables.inc.php"; require_once "core/functions/functions.php"; require_once "core/functions/datetime_functions.php"; require_once "core/functions/product_functions.php"; require_once "core/functions/category_functions.php"; require_once "core/includes/database/mysql.php"; error_reporting (E_ALL ^ E_NOTICE); db_connect(DB_HOST,DB_USER,DB_PASS); db_select_db(DB_NAME); $doc = simplexml_load_file('import.xml'); foreach ($doc->categories->category as $item) { $parent_id = 1; if ( $item["parentId"] ) { foreach ($doc->categories->category as $tmp) { if ( (int)$tmp["id"] == (int)$item["parentId"] ) { $q = db_query("SELECT categoryID FROM ".DB_PRFX."categories WHERE name='".iconv('UTF-8','CP1251', $tmp[0])."' ") or die(mysql_error()); if ($row = db_fetch_row($q) ) $parent_id = (int)$row["categoryID"]; } } } $sql= "insert into ".DB_PRFX."categories (name, parent, products_count, description, picture, ". " products_count_admin, sort_order, allow_products_comparison, allow_products_search, show_subcategories_products, ". " meta_description, meta_keywords, title, subcount ) "." VALUES ('".trim(iconv('UTF-8','CP1251', $item[0]))."', ". (int)$parent_id.",0 ,'' ,'' ,0 , 0, 1, 1, 1, '', '', '".trim(iconv('UTF-8','CP1251', $item[0]))."', 0);"; db_query($sql); } echo "Категории созданы\n"; foreach ($doc->offers->offer as $item) { $product_category_id = 1; foreach ($doc->categories->category as $tmp) { if ( (int)$tmp["id"] == (int)$item->categoryId ) { $q = db_query("SELECT categoryID FROM ".DB_PRFX."categories WHERE name='".iconv('UTF-8','CP1251', $tmp[0])."' ") or die(mysql_error()); if ($row = db_fetch_row($q) ) $product_category_id = (int)$row["categoryID"]; } } $res = AddProduct($product_category_id, xToText(iconv('UTF-8','CP1251', $item->model)), (int)$item->price, '', 0, '', 0, xToText($item["id"]), 0, isset($_POST["ProductIsProgram"]), "eproduct_filename", 7, 5, 0, '', '', isset($_POST["free_shipping"]), 1, 0, null, xToText(iconv('UTF-8','CP1251', $item->model))); } update_psCount(1); echo "Товары созданы\n"; ?>
формат xml файла под данный кусок кода
<?xml version="1.0" encoding="windows-1251"?> <yml_catalog date="2013-02-11 14:50"> <currencies> <currency id="RUR" rate="1"/> </currencies> <categories> <category id="2">Краски</category> <category id="3" parentId="2">Готовые краски</category> <category id="4" parentId="2">Специальные краски</category> <category id="5">Эксклюзивные краски</category> <category id="6" parentId="5">Суперкраска</category> </categories> <offers> <offer id="000000000000000000000001" available="True"> <price>1050</price> <currencyId>RUR</currencyId> <model>Краска 1</model> <categoryId>3</categoryId> </offer> <offer id="000000000000000000000002" available="True"> <price>1260</price> <currencyId>RUR</currencyId> <model>Краска 2</model> <categoryId>3</categoryId> </offer> <offer id="000000000000000000000004" available="True"> <price>3000</price> <currencyId>RUR</currencyId> <model>Спец. краска 1</model> <categoryId>4</categoryId> </offer> <offer id="000000000000000000000005" available="True"> <price>3750</price> <currencyId>RUR</currencyId> <model>Спец. краска 2</model> <categoryId>4</categoryId> </offer> </offers> </yml_catalog>
заготовка легко адаптируется под любой xml формат, если с кодировкой плохо, убираем функцию iconv (естественно оставляя параметр)