Dodaj funkcję importu produktów z XML oraz aktualizację cen w bazie danych

This commit is contained in:
2025-06-03 00:46:45 +02:00
parent b7a0482fd0
commit 6e424dd198
2 changed files with 190 additions and 10 deletions

View File

@@ -1082,7 +1082,7 @@
},
".htaccess": {
"type": "-",
"size": 7171,
"size": 5862,
"lmtime": 1746726321526,
"modified": true
},
@@ -1104,6 +1104,12 @@
"lmtime": 0,
"modified": false
},
".htaccess.2025-05-22-1747946135": {
"type": "-",
"size": 7171,
"lmtime": 0,
"modified": false
},
".htaccess_backup_20250107_134938": {
"type": "-",
"size": 14791,
@@ -2535,9 +2541,9 @@
},
"import-product.php": {
"type": "-",
"size": 22553,
"lmtime": 1744921788328,
"modified": true
"size": 32802,
"lmtime": 1748902490178,
"modified": false
},
"index.php": {
"type": "-",
@@ -8521,14 +8527,14 @@
},
"dr_materac.css": {
"type": "-",
"size": 83953,
"lmtime": 1747260838232,
"size": 83952,
"lmtime": 1748716771022,
"modified": false
},
"dr_materac.css.map": {
"type": "-",
"size": 234973,
"lmtime": 1747260838232,
"size": 31537,
"lmtime": 1748716771023,
"modified": false
},
"dr_materac.css.sync-conflict-20231027-200011-EDGUH2C.map": {
@@ -8539,8 +8545,8 @@
},
"dr_materac.scss": {
"type": "-",
"size": 107193,
"lmtime": 1747260837430,
"size": 100108,
"lmtime": 1748716771025,
"modified": false
},
"dr_materac.sync-conflict-20231027-195749-EDGUH2C.css": {

View File

@@ -3,6 +3,180 @@
include(dirname(__FILE__) . '/config/config.inc.php');
include(dirname(__FILE__) . '/init.php');
libxml_use_internal_errors(true);
$xmlString = file_get_contents( 'https://amz.com.pl/bazy-produktow-export/produkty-amz.xml' );
if ( !$xmlString ) {
die("Error: Cannot load XML file.");
}
$xmlString = preg_replace( '/(<\/?)(\w+):([^>]*>)/', '$1$2_$3', $xmlString );
// replace '-' with '_' in XML tags
$xml = simplexml_load_string( $xmlString, 'SimpleXMLElement', LIBXML_NOCDATA );
if ( !$xml )
{
die("Error: Cannot parse XML");
}
$products_array = [];
// pętla po produktach data -> post
foreach ( $xml -> post as $productData )
{
$product_tmp = (array)$productData;
$title = $product_tmp['Nazwaproduktu'];
$title_md5 = md5( $title );
if ( strpos( $product_tmp['Dostępnerozmiaryjeślidotyczy:untitled_3'], '|' ) !== false )
{
$products_array[$title_md5]['ean'] = $product_tmp['KodEAN-13'];
$products_array[$title_md5]['title'] = $title;
$products_array[$title_md5]['price'] = floatval( str_replace( ',', '', $product_tmp['CenaBRUTTO'] ) ) / 1.23; // cena netto
}
else
{
$combination_tmp['ean'] = $product_tmp['KodEAN-13'];
$combination_tmp['price'] = floatval( str_replace( ',', '', $product_tmp['CenaBRUTTO'] ) ) / 1.23; // cena netto
if ( isset( $products_array[$title_md5] ) )
$products_array[$title_md5]['combinations'][] = $combination_tmp;
else
{
$products_array[$title_md5]['ean'] = $product_tmp['KodEAN-13'];
$products_array[$title_md5]['title'] = $title;
$products_array[$title_md5]['price'] = floatval( str_replace( ',', '', $product_tmp['CenaBRUTTO'] ) ) / 1.23; // cena netto
}
}
}
foreach ( $products_array as $product )
{
if ( !count( $product['combinations'] ) )
$price = $product['price'];
else
$price = 0; // jeśli są kombinacje, to cena jest 0
$reference = $product['ean'];
$id_tax_rules_group = 62; // ID grupy reguł podatkowych
$active = 1; // Ustawienie produktu jako nieaktywny
$sql = 'UPDATE `' . _DB_PREFIX_ . 'product`
SET `price` = ' . (float)$price . ',
`id_tax_rules_group` = ' . (int)$id_tax_rules_group . ',
`active` = ' . (int)$active . '
WHERE `reference` = \'' . pSQL($reference) . '\'';
$result = Db::getInstance()->execute($sql);
$result = Db::getInstance()->executeS('SELECT id_product FROM `' . _DB_PREFIX_ . 'product` WHERE `reference` = \'' . pSQL($reference) . '\'');
// $id_product może być tablicą
if ( is_array( $result ) )
{
foreach ( $result as $id )
{
// update materac_product_shop where id_product = id_product
$sql_shop = 'UPDATE `' . _DB_PREFIX_ . 'product_shop`
SET `price` = ' . (float)$price . ',
`id_tax_rules_group` = ' . (int)$id_tax_rules_group . ',
`active` = ' . (int)$active . '
WHERE `id_product` = ' . $id['id_product'];
$result_shop = Db::getInstance()->execute($sql_shop);
echo "Zaktualizowano produkt z referencją $reference, id_product: " . $id['id_product'] . ", cena: $price, id_tax_rules_group: $id_tax_rules_group, active: $active<br>";
}
}
else
{
// update materac_product_shop where id_product = id_product
$sql_shop = 'UPDATE `' . _DB_PREFIX_ . 'product_shop`
SET `price` = ' . (float)$price . ',
`id_tax_rules_group` = ' . (int)$id_tax_rules_group . ',
`active` = ' . (int)$active . '
WHERE `id_product` = ' . (int)$result;
$result_shop = Db::getInstance()->execute($sql_shop);
if ( !$result_shop )
echo "Zaktualizowano produkt z referencją $reference, id_product: $result, cena: $price, id_tax_rules_group: $id_tax_rules_group, active: $active<br>";
}
// update materac_product_attribute by reference
foreach ( $product['combinations'] as $combination )
{
$sql_combination = 'UPDATE `' . _DB_PREFIX_ . 'product_attribute`
SET `price` = ' . (float)$combination['price'] . '
WHERE `reference` = \'' . pSQL($combination['ean']) . '\'';
$result_combination = Db::getInstance()->execute($sql_combination);
// get id_product_attribute from materac_product_attribute where reference = KodEAN-13
$id_product_attribute = Db::getInstance()->getValue('SELECT id_product_attribute FROM `' . _DB_PREFIX_ . 'product_attribute` WHERE `reference` = \'' . pSQL($combination['ean']) . '\'');
if ( !$result_combination )
{
echo "Error: Cannot update product attribute with reference " . $combination['ean'] . "<br>";
continue;
}
// update materac_product_attribute_shop where id_product_attribute = id_product_attribute
$sql_combination_shop = 'UPDATE `' . _DB_PREFIX_ . 'product_attribute_shop`
SET `price` = ' . (float)$combination['price'] . '
WHERE `id_product_attribute` = ' . (int)$id_product_attribute;
$result_combination_shop = Db::getInstance()->execute($sql_combination_shop);
}
echo "Zaktualizowano kombinacje dla produktu z referencją $reference<br>";
}
exit;
// if ( strpos( $product_tmp['Dostępnerozmiaryjeślidotyczy:untitled_3'], '|' ) !== false )
// continue;
// // update price, id_tax_rules_group, active in materac_product where reference = KodEAN-13
// $reference = $product_tmp['KodEAN-13'];
// $price = floatval( str_replace( ',', '', $product_tmp['CenaBRUTTO'] ) ) / 1.23; // cena netto
// $id_tax_rules_group = 62; // ID grupy reguł podatkowych
// $active = 0; // Ustawienie produktu jako aktywnego
// $sql = 'UPDATE `' . _DB_PREFIX_ . 'product`
// SET `price` = ' . 0 . ',
// `id_tax_rules_group` = ' . (int)$id_tax_rules_group . ',
// `active` = ' . (int)$active . '
// WHERE `reference` = \'' . pSQL($reference) . '\'';
// $result = Db::getInstance()->execute($sql);
// // get id_product from materac_product where reference = KodEAN-13
// $id_product = Db::getInstance()->getValue('SELECT id_product FROM `' . _DB_PREFIX_ . 'product` WHERE `reference` = \'' . pSQL($reference) . '\'');
// if ( !$result )
// {
// echo "Error: Cannot update product with reference $reference";
// continue;
// }
// // update materac_product_shop where id_product = id_product
// $sql_shop = 'UPDATE `' . _DB_PREFIX_ . 'product_shop`
// SET `price` = ' . 0 . ',
// `id_tax_rules_group` = ' . (int)$id_tax_rules_group . ',
// `active` = ' . (int)$active . '
// WHERE `id_product` = ' . (int)$id_product;
// $result_shop = Db::getInstance()->execute($sql_shop);
// if ( !$result_shop )
// {
// echo "Error: Cannot update product shop with id_product $id_product";
// continue;
// }
// echo "Zaktualizowano produkt z referencją $reference, id_product: $id_product, cena: $price, id_tax_rules_group: $id_tax_rules_group, active: $active<br>";
// /// update materac_product_attribute by reference
// $sql_combination = 'UPDATE `' . _DB_PREFIX_ . 'product_attribute`
// SET `price` = ' . (float)$price . '
// WHERE `reference` = \'' . pSQL($reference) . '\'';
// $result_combination = Db::getInstance()->execute($sql_combination);
// if ( !$result_combination )
// {
// echo "Error: Cannot update product attribute with reference $reference";
// continue;
// }
// }
// echo '<pre>';
// print_r( $products_array );
// // exit;
// exit;
// Pobierz aktualny język kontekstu (ustaw odpowiedni ID języka, np. 1 dla polskiego)
$id_lang = Context::getContext()->language->id;