versionecologique/scripts/import_products.php
2017-07-24 10:04:12 +02:00

427 lines
11 KiB
PHP

<?php
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
require_once dirname(__FILE__).'/../www/config/config.inc.php';
// Old database
$configOld = array(
'dsn' => 'mysql:dbname=versionecologique_old;host=192.168.0.41;charset=UTF8',
'user' => 'root',
'pass' => '',
);
$dbOld = new PDO($configOld['dsn'], $configOld['user'], $configOld['pass']);
// New database
$configNew = array(
'dsn' => 'mysql:dbname='._DB_NAME_.';host='._DB_SERVER_.';charset=UTF8',
'user' => _DB_USER_,
'pass' => _DB_PASSWD_,
);
$dbNew = new PDO($configNew['dsn'], $configNew['user'], $configNew['pass']);
// Get all categories import in new DB - start import_categories before
$categories = array();
$sql = "SELECT id_category FROM ps_category";
$stmtNew = $dbNew->query($sql);
if ($stmtNew->rowCount() > 0) {
while ($item = $stmtNew->fetch(PDO::FETCH_OBJ)) {
$categories[] = $item->id_category;
}
}
// Get products in category (old db)
$sql = "SELECT DISTINCT(id_product) AS id_product FROM ps_category_product WHERE id_category IN (".join(',', $categories).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No products ?\n"; exit;
} else {
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$products[] = $item->id_product;
}
}
// Truncate
$tables = array(
'ps_category_product',
'ps_product',
'ps_product_lang',
'ps_product_shop',
'ps_stock_available',
);
foreach($tables as $table) {
$dbNew->query('TRUNCATE TABLE '.$table);
}
// ps_category_product
$table = 'ps_category_product';
$cols = array(
'id_category',
'id_product',
'position',
);
echo "Importing $table...\n";
$sql = "SELECT * FROM ".$table." WHERE id_product IN (".join(',', $products).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No Results ?\n";
} else {
try {
// Construct SQL
$sql = 'INSERT INTO '.$table.' (';
foreach ($cols as $c) {
$sql.= '`'.$c.'`, ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ') VALUES (';
foreach ($cols as $c) {
$sql.= ':'.$c.', ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ')';
// Start transaction
$dbNew->beginTransaction();
$i = 0;
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$stmtImport = $dbNew->prepare($sql);
foreach ($cols as $c) {
$stmtImport->bindValue($c, $item->{$c});
}
if ($stmtImport->execute() === false) {
echo "Error on $table - $i\n";
exit;
}
$i++;
}
if ($dbNew->commit() === false) {
echo "Error on $table\n";
exit;
}
} catch(Exception $e) {
$dbNew->rollBack();
echo "Error on $table\n";
exit;
}
echo $stmtOld->rowCount(). " / " . $i . "\n";
}
echo "End of importing $table\n";
// ps_product
$table = 'ps_product';
$cols = array(
'id_product',
'id_supplier',
'id_manufacturer',
'id_category_default',
'id_shop_default',
'id_tax_rules_group',
'on_sale',
'online_only',
'ean13',
'upc',
'ecotax',
'quantity',
'minimal_quantity',
'price',
'wholesale_price',
'unity',
'unit_price_ratio',
'additional_shipping_cost',
'reference',
'supplier_reference',
'location',
'width',
'height',
'depth',
'weight',
'out_of_stock',
'quantity_discount',
'customizable',
'uploadable_files',
'text_fields',
'active',
'redirect_type',
'id_product_redirected',
'available_for_order',
'available_date',
'condition',
'show_price',
/*'favorites_product',
'new_product',*/
'indexed',
'visibility',
'cache_is_pack',
'cache_has_attachments',
'is_virtual',
'cache_default_attribute',
'date_add',
'date_upd',
'advanced_stock_management',
'pack_stock_type',
);
echo "Importing $table...\n";
$sql = "SELECT * FROM ".$table." WHERE id_product IN (".join(',', $products).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No Results ?\n";
} else {
try {
// Construct SQL
$sql = 'INSERT INTO '.$table.' (';
foreach ($cols as $c) {
$sql.= '`'.$c.'`, ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ') VALUES (';
foreach ($cols as $c) {
$sql.= ':'.$c.', ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ')';
// Start transaction
$dbNew->beginTransaction();
$i = 0;
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$stmtImport = $dbNew->prepare($sql);
foreach ($cols as $c) {
$stmtImport->bindValue($c, $item->{$c});
}
if ($stmtImport->execute() === false) {
echo "Error on $table - $i\n";
exit;
}
$i++;
}
if ($dbNew->commit() === false) {
echo "Error on $table\n";
exit;
}
} catch(Exception $e) {
$dbNew->rollBack();
echo "Error on $table\n";
exit;
}
echo $stmtOld->rowCount(). " / " . $i . "\n";
}
echo "End of importing $table\n";
// ps_product_lang => from id_product
$table = 'ps_product_lang';
$cols = array(
'id_product',
'id_shop',
'id_lang',
'description',
'description_short',
'link_rewrite',
'meta_description',
'meta_keywords',
'meta_title',
'name',
'available_now',
'available_later',
);
echo "Importing $table...\n";
$sql = "SELECT * FROM ".$table." WHERE id_product IN (".join(',', $products).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No Results ?\n";
} else {
try {
// Construct SQL
$sql = 'INSERT INTO '.$table.' (';
foreach ($cols as $c) {
$sql.= '`'.$c.'`, ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ') VALUES (';
foreach ($cols as $c) {
$sql.= ':'.$c.', ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ')';
// Start transaction
$dbNew->beginTransaction();
$i = 0;
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$stmtImport = $dbNew->prepare($sql);
foreach ($cols as $c) {
$stmtImport->bindValue($c, $item->{$c});
}
if ($stmtImport->execute() === false) {
echo "Error on $table - $i\n";
exit;
}
$i++;
}
if ($dbNew->commit() === false) {
echo "Error on $table\n";
exit;
}
} catch(Exception $e) {
$dbNew->rollBack();
echo "Error on $table\n";
exit;
}
echo $stmtOld->rowCount(). " / " . $i . "\n";
}
echo "End of importing $table\n";
// ps_product_shop
$table = 'ps_product_shop';
$cols = array(
'id_product' => null,
'id_shop' => null,
'id_category_default' => null,
'id_tax_rules_group' => null,
'on_sale' => null,
'online_only' => null,
'ecotax' => null,
'minimal_quantity' => null,
'price' => null,
'wholesale_price' => null,
'unity' => null,
'unit_price_ratio' => null,
'additional_shipping_cost' => null,
'customizable' => null,
'uploadable_files' => null,
'text_fields' => null,
'active' => null,
'redirect_type' => null,
'id_product_redirected' => null,
'available_for_order' => array( 'value' => 1 ),
//'favorites_product' => null,
//'new_product' => null,
'available_date' => null,
'condition' => null,
'show_price' => array( 'value' => 1 ),
'indexed' => null,
'visibility' => null,
'cache_default_attribute' => null,
'advanced_stock_management' => null,
'date_add' => null,
'date_upd' => null,
'pack_stock_type' => null,
);
echo "Importing $table...\n";
$sql = "SELECT * FROM ".$table." WHERE id_product IN (".join(',', $products).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No Results ?\n";
} else {
try {
// Construct SQL
$sql = 'INSERT INTO '.$table.' (';
foreach ($cols as $c => $v) {
$sql.= '`'.$c.'`, ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ') VALUES (';
foreach ($cols as $c => $v) {
$sql.= ':'.$c.', ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ')';
// Start transaction
$dbNew->beginTransaction();
$i = 0;
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$stmtImport = $dbNew->prepare($sql);
foreach ($cols as $c => $v) {
if (is_array($v)) {
$stmtImport->bindValue($c, $v['value']);
} else {
$stmtImport->bindValue($c, $item->{$c});
}
}
if ($stmtImport->execute() === false) {
echo "Error on $table - $i\n";
exit;
}
$i++;
}
if ($dbNew->commit() === false) {
echo "Error on $table\n";
exit;
}
} catch(Exception $e) {
$dbNew->rollBack();
echo "Error on $table\n";
exit;
}
echo $stmtOld->rowCount(). " / " . $i . "\n";
}
echo "End of importing $table\n";
// ps_stock_available => from id_product
$table = 'ps_stock_available';
$cols = array(
'id_stock_available',
'id_product',
'id_product_attribute',
'id_shop',
'id_shop_group',
'quantity',
'depends_on_stock',
'out_of_stock',
);
echo "Importing $table...\n";
$sql = "SELECT * FROM ".$table." WHERE id_product IN (".join(',', $products).")";
$stmtOld = $dbOld->query($sql);
if ($stmtOld->rowCount() == 0) {
echo "No Results ?\n";
} else {
try {
// Construct SQL
$sql = 'INSERT INTO '.$table.' (';
foreach ($cols as $c) {
$sql.= '`'.$c.'`, ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ') VALUES (';
foreach ($cols as $c) {
$sql.= ':'.$c.', ';
}
$sql = trim($sql);
$sql = trim($sql, ',');
$sql.= ')';
// Start transaction
$dbNew->beginTransaction();
$i = 0;
while($item = $stmtOld->fetch(PDO::FETCH_OBJ)) {
$stmtImport = $dbNew->prepare($sql);
foreach ($cols as $c) {
$stmtImport->bindValue($c, $item->{$c});
}
if ($stmtImport->execute() === false) {
echo "Error on $table - $i\n";
exit;
}
$i++;
}
if ($dbNew->commit() === false) {
echo "Error on $table\n";
exit;
}
} catch(Exception $e) {
$dbNew->rollBack();
echo "Error on $table\n";
exit;
}
echo $stmtOld->rowCount(). " / " . $i . "\n";
}
echo "End of importing $table\n";