Dodano funkcję do pobierania wszystkich adresów URL obrazów z danych produktu XML oraz zaktualizowano logikę dodawania obrazów do produktów. Zmieniono domyślną grupę VAT dla produktów oraz poprawiono aktualizację cen wariantów.

This commit is contained in:
2025-12-18 16:03:35 +01:00
parent fb4265c60e
commit 0a047ea0ba
2 changed files with 428 additions and 174 deletions

View File

@@ -26,7 +26,7 @@
"cache": {},
"-cart_default.jpg": {
"type": "-",
"size": 2720,
"size": 2369,
"lmtime": 0,
"modified": true
},
@@ -61,15 +61,15 @@
},
"-home_default.jpg": {
"type": "-",
"size": 7402,
"size": 4937,
"lmtime": 0,
"modified": true
},
".htaccess": {
"type": "-",
"size": 7691,
"size": 7693,
"lmtime": 0,
"modified": false
"modified": true
},
".htaccess.2025-05-22-1747946145": {
"type": "-",
@@ -134,8 +134,8 @@
"img": {},
"import-drewmax.php": {
"type": "-",
"size": 32286,
"lmtime": 1765839405797,
"size": 34786,
"lmtime": 1766057916974,
"modified": false
},
"import-product.php": {
@@ -164,7 +164,7 @@
},
".jpg": {
"type": "-",
"size": 440848,
"size": 273351,
"lmtime": 0,
"modified": true
},
@@ -177,7 +177,7 @@
},
"-large_default.jpg": {
"type": "-",
"size": 61644,
"size": 25527,
"lmtime": 0,
"modified": true
},
@@ -197,7 +197,7 @@
},
"-medium_default.jpg": {
"type": "-",
"size": 20567,
"size": 10596,
"lmtime": 0,
"modified": true
},
@@ -206,19 +206,19 @@
"CHANGELOG": {
"type": "-",
"size": 5258,
"lmtime": 1764184727462,
"lmtime": 0,
"modified": false
},
"cleanup.php": {
"type": "-",
"size": 350,
"lmtime": 1764184727462,
"lmtime": 0,
"modified": false
},
"config_pl.xml": {
"type": "-",
"size": 556,
"lmtime": 1764184727462,
"lmtime": 1766048950150,
"modified": false
},
"controllers": {
@@ -226,13 +226,7 @@
"cron.php": {
"type": "-",
"size": 4477,
"lmtime": 1765828878778,
"modified": false
},
"index.php": {
"type": "-",
"size": 1321,
"lmtime": 1764184727464,
"lmtime": 1766048950152,
"modified": false
}
}
@@ -240,7 +234,7 @@
"cron.php": {
"type": "-",
"size": 1492,
"lmtime": 1764184727464,
"lmtime": 0,
"modified": false
},
".DS_Store": {
@@ -252,185 +246,79 @@
"fill.php": {
"type": "-",
"size": 348,
"lmtime": 1764184727465,
"lmtime": 0,
"modified": false
},
"gm_omniprice.php": {
"type": "-",
"size": 100447,
"lmtime": 1765828878780,
"lmtime": 1766048950155,
"modified": false
},
"index.php": {
"type": "-",
"size": 1304,
"lmtime": 1764184727466,
"lmtime": 0,
"modified": false
},
"logo.png": {
"type": "-",
"size": 4539,
"lmtime": 1764184727466,
"lmtime": 0,
"modified": false
},
"logo.webp": {
"type": "-",
"size": 1980,
"lmtime": 1764184727467,
"lmtime": 0,
"modified": false
},
"template.php": {
"type": "-",
"size": 1926,
"lmtime": 1764184727467,
"lmtime": 0,
"modified": false
},
"translations": {
"de.php": {
"type": "-",
"size": 9540,
"lmtime": 1764184727468,
"lmtime": 0,
"modified": false
},
"es.php": {
"type": "-",
"size": 9589,
"lmtime": 1764184727469,
"lmtime": 0,
"modified": false
},
"fr.php": {
"type": "-",
"size": 9585,
"lmtime": 1764184727469,
"lmtime": 0,
"modified": false
},
"it.php": {
"type": "-",
"size": 9493,
"lmtime": 1764184727470,
"lmtime": 0,
"modified": false
},
"nl.php": {
"type": "-",
"size": 0,
"lmtime": 1764184727470,
"lmtime": 0,
"modified": false
},
"pl.php": {
"type": "-",
"size": 9307,
"lmtime": 1764184727471,
"lmtime": 1766048950156,
"modified": false
}
},
"upgrade": {
"upgrade-1.0.10.php": {
"type": "-",
"size": 170,
"lmtime": 1764184727471,
"modified": false
},
"upgrade-1.0.11.php": {
"type": "-",
"size": 170,
"lmtime": 1764184727472,
"modified": false
},
"upgrade-1.0.13.php": {
"type": "-",
"size": 333,
"lmtime": 1764184727472,
"modified": false
},
"upgrade-1.1.0.php": {
"type": "-",
"size": 195,
"lmtime": 1764184727473,
"modified": false
},
"upgrade-1.1.11.php": {
"type": "-",
"size": 196,
"lmtime": 1764184727473,
"modified": false
},
"upgrade-1.2.0.php": {
"type": "-",
"size": 191,
"lmtime": 1764184727473,
"modified": false
},
"upgrade-1.2.3.php": {
"type": "-",
"size": 190,
"lmtime": 1764184727474,
"modified": false
}
},
"views": {
"css": {
"gm_omniprice.css": {
"type": "-",
"size": 87,
"lmtime": 1764184727475,
"modified": false
},
"index.php": {
"type": "-",
"size": 1304,
"lmtime": 1764184727475,
"modified": false
}
},
"index.php": {
"type": "-",
"size": 1304,
"lmtime": 1764184727475,
"modified": false
},
"js": {
"gm_omniprice.js": {
"type": "-",
"size": 543,
"lmtime": 1764184727476,
"modified": false
},
"index.php": {
"type": "-",
"size": 1304,
"lmtime": 1764184727476,
"modified": false
}
},
"templates": {
"hook": {
"index.php": {
"type": "-",
"size": 1304,
"lmtime": 1764184727477,
"modified": false
},
"price.tpl": {
"type": "-",
"size": 516,
"lmtime": 1764184727478,
"modified": false
},
"tab.tpl": {
"type": "-",
"size": 1009,
"lmtime": 1764184727478,
"modified": false
}
},
"index.php": {
"type": "-",
"size": 1304,
"lmtime": 1764184727478,
"modified": false
}
}
}
"upgrade": {},
"views": {}
}
},
"override": {
@@ -522,32 +410,327 @@
"sitemaps": {},
"sitemap_shop_1.xml": {
"type": "-",
"size": 686,
"size": 827,
"lmtime": 0,
"modified": false
"modified": true
},
"-small_default.jpg": {
"type": "-",
"size": 2056,
"size": 1818,
"lmtime": 0,
"modified": true
},
"src": {},
"themes": {},
"themes": {
"classic": {
"assets": {
"css": {
"00b1fb69fb78ff50fd555de80b2fb45e.ttf": {
"type": "-",
"size": 174048,
"lmtime": 0,
"modified": false
},
"016d41b1da3f1f5aaa44a7e20baee117.woff": {
"type": "-",
"size": 32904,
"lmtime": 0,
"modified": false
},
"04be689c12d686c595032705f6b3cf6c.svg": {
"type": "-",
"size": 848,
"lmtime": 0,
"modified": false
},
"057cc3c927dc0b2e8dbb739a306bd3a3.otf": {
"type": "-",
"size": 73016,
"lmtime": 0,
"modified": false
},
"0ba57b3ea460c3d7d0c46d6138f088a8.svg": {
"type": "-",
"size": 3780,
"lmtime": 0,
"modified": false
},
"0f099f4e4d67aeb18ab6da291367a93f.woff": {
"type": "-",
"size": 32596,
"lmtime": 0,
"modified": false
},
"12a47ed5fd5585f0f4227fa035a1a607.woff2": {
"type": "-",
"size": 60840,
"lmtime": 0,
"modified": false
},
"1e81f33d197ccdb39d4edce581ff50ec.woff": {
"type": "-",
"size": 30148,
"lmtime": 0,
"modified": false
},
"250c120c3f8b2b0bb58ea27ee36153fc.woff2": {
"type": "-",
"size": 29860,
"lmtime": 0,
"modified": false
},
"29acb3c63123183305b02ebc9844b2ef.otf": {
"type": "-",
"size": 73124,
"lmtime": 0,
"modified": false
},
"33e7446832ac7aa84b959a6ea8efb915.svg": {
"type": "-",
"size": 891,
"lmtime": 0,
"modified": false
},
"38fa559ad8e8cbaee0c7a2178d308df5.woff2": {
"type": "-",
"size": 27760,
"lmtime": 0,
"modified": false
},
"3a94078d659136527abfe39bc3432628.svg": {
"type": "-",
"size": 1013,
"lmtime": 0,
"modified": false
},
"40ef5dc59db6eeef16942fc6a0baa8de.otf": {
"type": "-",
"size": 71000,
"lmtime": 0,
"modified": false
},
"479700ea91d964d295d75c6b67bd0b28.eot": {
"type": "-",
"size": 69177,
"lmtime": 0,
"modified": false
},
"47f13bcaf75af86613bca9721d6e858f.svg": {
"type": "-",
"size": 901,
"lmtime": 0,
"modified": false
},
"4903fb37c04676f5a805be194ad59b2b.svg": {
"type": "-",
"size": 1560,
"lmtime": 0,
"modified": false
},
"4db1ea0d15010c761ee577d7c0f5a0a0.otf": {
"type": "-",
"size": 70960,
"lmtime": 0,
"modified": false
},
"52260854eea2d38939bf40e9e9687830.woff2": {
"type": "-",
"size": 29756,
"lmtime": 0,
"modified": false
},
"56b65c6bec8f40445da4f7853f2553ec.otf": {
"type": "-",
"size": 70940,
"lmtime": 0,
"modified": false
},
"57a1d494b0e76131f062c5c9c2d4960d.woff2": {
"type": "-",
"size": 30220,
"lmtime": 0,
"modified": false
},
"64041bf784575b30a79fa31900c79cf6.otf": {
"type": "-",
"size": 70404,
"lmtime": 0,
"modified": false
},
"726b87682d18d1e8307635d146fe58cc.svg": {
"type": "-",
"size": 3103,
"lmtime": 0,
"modified": false
},
"740f1557cae714dc4b180b4d5de69375.svg": {
"type": "-",
"size": 5233,
"lmtime": 0,
"modified": false
},
"7acb0ff3a9a16ba3806d7f16b745af38.svg": {
"type": "-",
"size": 1192,
"lmtime": 0,
"modified": false
},
"83131b9daba3e9a7b2c7ae7e47d2d503.woff": {
"type": "-",
"size": 31392,
"lmtime": 0,
"modified": false
},
"882e1291e47c7d9d5dd7633845caa204.svg": {
"type": "-",
"size": 891,
"lmtime": 0,
"modified": false
},
"895e092292d88717adaa347e532822ab.woff2": {
"type": "-",
"size": 28776,
"lmtime": 0,
"modified": false
},
"91fc2885f1aa34fb73f63e9ef538fe1c.svg": {
"type": "-",
"size": 1145,
"lmtime": 0,
"modified": false
},
"a7360927bb3673ed7814d6f607d122cb.woff2": {
"type": "-",
"size": 29856,
"lmtime": 0,
"modified": false
},
"a8af1233a958bd1e96edcad65791a0d3.woff": {
"type": "-",
"size": 32840,
"lmtime": 0,
"modified": false
},
"b252849e892c264f4f220c7e7cb0207a.svg": {
"type": "-",
"size": 1198,
"lmtime": 0,
"modified": false
},
"b2efa71e4ca1cd26b0f4df4e71e531a8.svg": {
"type": "-",
"size": 357,
"lmtime": 0,
"modified": false
},
"c472ea2ef3e5b37438730a4ab00513b4.svg": {
"type": "-",
"size": 901,
"lmtime": 0,
"modified": false
},
"c80d4fb11dcc2140b26007ce39cfa521.woff": {
"type": "-",
"size": 32508,
"lmtime": 0,
"modified": false
},
"cbeba06698c00a61e82219b76357ce42.svg": {
"type": "-",
"size": 1020,
"lmtime": 0,
"modified": false
},
"custom.css": {
"type": "-",
"size": 91828,
"lmtime": 1766048950158,
"modified": false
},
"custom.css.map": {
"type": "-",
"size": 25242,
"lmtime": 0,
"modified": false
},
"custom.scss": {
"type": "-",
"size": 79358,
"lmtime": 1766048950160,
"modified": false
},
"dce0916af972e3040aa191f74b17dd46.woff2": {
"type": "-",
"size": 30428,
"lmtime": 0,
"modified": false
},
"de69cd9e672c81725abcde04ecf022ee.woff": {
"type": "-",
"size": 33400,
"lmtime": 0,
"modified": false
},
"error.css": {
"type": "-",
"size": 742,
"lmtime": 0,
"modified": false
},
"f2a0933406f7830651524f477ba2f543.woff": {
"type": "-",
"size": 79612,
"lmtime": 0,
"modified": false
},
"f56d95737d55e2bdba95baa528b2583b.svg": {
"type": "-",
"size": 3780,
"lmtime": 0,
"modified": false
},
"fa3993a997a0b84e9900ee168b18b919.svg": {
"type": "-",
"size": 848,
"lmtime": 0,
"modified": false
},
"fd2d932b381003546821288ecde6e370.otf": {
"type": "-",
"size": 71180,
"lmtime": 0,
"modified": false
},
"theme.css": {
"type": "-",
"size": 192439,
"lmtime": 0,
"modified": false
},
"theme.css.map": {
"type": "-",
"size": 544940,
"lmtime": 0,
"modified": false
},
"theme.scss": {
"type": "-",
"size": 255419,
"lmtime": 0,
"modified": false
}
}
}
}
},
"tmp": {},
"tools": {},
"translations": {},
"update_price_log.csv": {
"type": "-",
<<<<<<< HEAD
"size": 23674,
"lmtime": 1765784209430,
"modified": false
=======
"size": 24168,
"lmtime": 0,
"lmtime": 1765784209430,
"modified": true
>>>>>>> f00bc6bbc467025557df943e2844498f47b8174d
},
"upload": {},
"var": {},

View File

@@ -111,18 +111,56 @@ function getCategoryId($categoryName) {
}
}
function getAllImageUrlsFromXmlProduct($productData, $max = 10)
{
$urls = [];
// główne <image>
if (!empty($productData->image)) {
$urls[] = trim((string)$productData->image);
}
// images_1..10 oraz Images_1..10 (różna wielkość liter w XML!)
for ($i = 1; $i <= $max; $i++) {
$k1 = 'images_' . $i;
$k2 = 'Images_' . $i;
$u1 = isset($productData->{$k1}) ? trim((string)$productData->{$k1}) : '';
$u2 = isset($productData->{$k2}) ? trim((string)$productData->{$k2}) : '';
if ($u1 !== '') $urls[] = $u1;
if ($u2 !== '') $urls[] = $u2;
}
// usuń duplikaty + puste
$urls = array_values(array_unique(array_filter($urls)));
return $urls;
}
// Function to download image from URL and associate it with a product
function addProductImage($productId, $imageUrl)
function addProductImage($productId, $imageUrl, $isCover = false)
{
$image = new Image();
$image->id_product = (int)$productId;
$image->position = Image::getHighestPosition($productId) + 1;
$image->cover = true; // Set the first image as cover
$image->cover = (bool)$isCover; // tylko pierwsze jako cover
$image->add();
$imagePath = $image->getPathForCreation();
$url = str_replace(' ', '%20', $imageUrl);
if (!copy($url, $imagePath . '.jpg')) {
// pobranie pliku
$url = str_replace(' ', '%20', trim($imageUrl));
// UWAGA: copy() czasem failuje na HTTPS; file_get_contents + file_put_contents bywa pewniejsze
$data = @file_get_contents($url);
if ($data === false) {
$image->delete();
return false;
}
if (@file_put_contents($imagePath . '.jpg', $data) === false) {
$image->delete();
return false;
}
@@ -296,7 +334,7 @@ function getTaxRulesGroupIdForRate($rate, $id_country = null) {
ORDER BY trg.`id_tax_rules_group` ASC';
$id = Db::getInstance()->getValue($sql);
return $id ? (int)$id : 0;
return $id ? (int)$id : 8;
}
// Zwraca główny produkt z grupy pierwszy, którego SKU istnieje w PrestaShop
@@ -322,7 +360,7 @@ foreach ($xml->product as $productData) {
}
// ID grupy VAT 23%
$idTaxRulesGroup23 = getTaxRulesGroupIdForRate(23);
$idTaxRulesGroup23 = 8;
// =======================================
// =========== TRYB AKTUALIZACJI =========
@@ -527,12 +565,14 @@ if ($modeAdd) {
// continue;
// }
// Główny produkt dane z pierwszego w grupie
// $mainProductData = $products[0];
// $mainProduct = findProductByReference((string)$mainProductData->sku);
// Główny produkt wybieramy deterministycznie
$mainProductData = findMainProductDataFromGroup($products);
if (!$mainProductData) {
$mainProductData = $products[0]; // fallback na etapie dodawania
// fallback: jeśli żaden nie istnieje, wybierz alfabetycznie najniższy SKU
usort($products, function($a, $b) {
return strcmp((string)$a->sku, (string)$b->sku);
});
$mainProductData = $products[0];
}
$mainProduct = findProductByReference((string)$mainProductData->sku);
@@ -552,9 +592,7 @@ if ($modeAdd) {
$mainProduct->price = $netPrice > 0 ? $netPrice : 0;
// VAT 23% jeśli dostępny
if (!empty($idTaxRulesGroup23)) {
$mainProduct->id_tax_rules_group = (int)$idTaxRulesGroup23;
}
$mainProduct->id_tax_rules_group = 8;
// Produkt aktywny + delivery times
$mainProduct->active = 1;
@@ -564,8 +602,14 @@ if ($modeAdd) {
$mainProduct->reference = (string)$mainProductData->sku;
$mainProduct->id_category_default = 107; // np. Meble
$mainProduct->link_rewrite = createLinkRewrite((string)$mainProductData->title);
// Najpierw dodaj produkt
$mainProduct->add();
$mainProduct = new Product($mainProduct->id);
$mainProduct->id_tax_rules_group = 8;
$mainProduct->update();
// ===== Poprawne przypisanie kategorii =====
$id_lang = (int)$context->language->id;
$defaultCategory = new Category($mainProduct->id_category_default, $id_lang);
@@ -584,15 +628,12 @@ if ($modeAdd) {
// ===== Koniec ustawienia kategorii =====
// Add images to the product
if (!empty($mainProductData->image)) {
addProductImage($mainProduct->id, (string)$mainProductData->image);
}
$imageUrls = getAllImageUrlsFromXmlProduct($mainProductData, 10);
for ($i = 1; $i <= 10; $i++) {
$imageUrl = (string)$mainProductData->{'images_' . $i};
if (!empty($imageUrl)) {
addProductImage($mainProduct->id, $imageUrl);
}
$first = true;
foreach ($imageUrls as $url) {
addProductImage($mainProduct->id, $url, $first);
$first = false;
}
$productAdded = true;
@@ -635,19 +676,40 @@ if ($modeAdd) {
// Add or update combination if it is unique
if (!empty($attributeIds) && !isset($addedCombinations[$key])) {
$combination = findCombinationByAttributes($mainProduct->id, $attributeIds);
// Oblicz cenę wariantu
$variantGross = parsePrice((string)$productData->price);
$variantNet = $variantGross > 0 ? Tools::ps_round($variantGross / 1.23, 6) : 0;
// Impact względem ceny bazowej produktu
$impact = $variantNet - $netPrice;
if ($impact < 0) {
$impact = 0; // zabezpieczenie
}
$impact = Tools::ps_round($impact, 6);
if (!$combination) {
// Create new combination
$combination = new Combination();
$combination->id_product = (int)$mainProduct->id;
$combination->quantity = 100; // startowo, i tak zaraz nadpiszemy StockAvailable
$combination->reference = (string)$productData->sku;
$combination->price = $impact; // wpływ na cenę (tax excluded)
$combination->ecotax = 0;
$combination->wholesale_price = 0;
$combination->default_on = 0;
$combination->add();
$combination->setAttributes($attributeIds);
$combination->save();
// Dodatkowy update aby upewnić się że cena jest prawidłowo zapisana
$combination->price = $impact;
$combination->update();
$combinationAdded = true;
} else {
// Update existing combination quantity if necessary
// Update existing combination
$combination->quantity = 100; // startowo
$combination->price = $impact; // aktualizacja ceny
$combination->update();
}
@@ -681,6 +743,15 @@ if ($modeAdd) {
$mainProduct->checkDefaultAttributes();
Product::updateDefaultAttribute($mainProduct->id);
// Upewnij się że produkt główny ma poprawne ustawienie VAT po dodaniu kombinacji
if ($productAdded || $combinationAdded) {
$mainProduct = new Product($mainProduct->id); // Przeładuj produkt
if (!empty($idTaxRulesGroup23) && (int)$mainProduct->id_tax_rules_group !== (int)$idTaxRulesGroup23) {
$mainProduct->id_tax_rules_group = (int)$idTaxRulesGroup23;
$mainProduct->update();
}
}
if ($productAdded || $combinationAdded) {
if ($productAdded) {
echo "<p>Dodałem produkt: " . htmlspecialchars((string)$mainProductData->title) . "</p>";