Enhance XML processing logic: update domain replacement and add custom_label_3 for sale items

This commit is contained in:
2025-12-05 22:43:41 +01:00
parent 5cae883a38
commit e7e6550b27

View File

@@ -42,28 +42,30 @@ if (!isset($namespaces['g']))
die('W pliku XML nie znaleziono przestrzeni nazw "g".'); die('W pliku XML nie znaleziono przestrzeni nazw "g".');
} }
// --- 3. Podmiana domeny w każdym <g:link> --- // --- 3. Podmiana domeny + logika ---
foreach ($xml->channel->item as $item) foreach ($xml->channel->item as $item)
{ {
// Dzieci w przestrzeni nazw g
$gChildren = $item->children($namespaces['g']); $gChildren = $item->children($namespaces['g']);
if (isset($gChildren->link)) if (isset($gChildren->link))
{ {
$currentLink = (string) $gChildren->link; $currentLink = (string) $gChildren->link;
// Podmiana samej domeny na nową
$newLink = str_replace($oldDomain, $newDomain, $currentLink); $newLink = str_replace($oldDomain, $newDomain, $currentLink);
$gChildren->link = $newLink; $gChildren->link = $newLink;
} }
// --- Zamiana price <-> sale_price jeśli sale_price istnieje --- // --- Jeśli produkt jest na wyprzedaży wybór custom_label_3 = sales ---
if (isset($gChildren->sale_price) && trim((string)$gChildren->sale_price) !== '') if (isset($gChildren->sale_price) && trim((string)$gChildren->sale_price) !== '')
{ {
// Dodaj custom_label_3 tylko jeśli nie istnieje
if (!isset($gChildren->custom_label_3)) {
$item->addChild('custom_label_3', 'sales', $namespaces['g']);
}
// Zamiana price <-> sale_price
$originalPrice = (string)$gChildren->price; $originalPrice = (string)$gChildren->price;
$salePrice = (string)$gChildren->sale_price; $salePrice = (string)$gChildren->sale_price;
// Zamiana wartości
$gChildren->price = $salePrice; $gChildren->price = $salePrice;
$gChildren->sale_price = $originalPrice; $gChildren->sale_price = $originalPrice;
} }
@@ -72,6 +74,7 @@ foreach ($xml->channel->item as $item)
{ {
$gChildren->identifier_exists = 'true'; $gChildren->identifier_exists = 'true';
} }
// --- Usuwanie emoji z opisu --- // --- Usuwanie emoji z opisu ---
if (isset($gChildren->description)) if (isset($gChildren->description))
{ {
@@ -80,13 +83,13 @@ foreach ($xml->channel->item as $item)
} }
} }
// --- 4. Zapis zmodyfikowanego XML na serwerze --- // --- 4. Zapis ---
if ($xml->asXML($outputFile) === false) if ($xml->asXML($outputFile) === false)
{ {
die('Nie udało się zapisać zmodyfikowanego pliku XML na serwerze.'); die('Nie udało się zapisać zmodyfikowanego pliku XML na serwerze.');
} }
// --- 5. Zbudowanie URL do zapisanego pliku i wyświetlenie go --- // --- 5. Informacja ---
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http'; $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost'; $host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); $path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');