Files
cdnPRO/innsi.pl/xml-convert.php
2024-10-25 16:01:25 +02:00

122 lines
3.7 KiB
PHP

<?php
$file = file_get_contents('https://sklep373302.shoparena.pl/console/integration/execute/name/GoogleProductSearch/lang/pl_PL');
$xml = new SimpleXMLElement($file);
$gid = 'g:id';
for ( $i = count( $xml -> entry ) - 1; $i >= 0; $i-- )
{
$id = $xml -> entry[$i] -> children( 'g', true ) -> id;
$ids_to_remove = array( 1831, 1832, 1833, 1834, 1834, 2738, 2737, 2739, 2740, 3079, 3080, 3081, 3082, 3083, 3084, 3085, 3086, 3087, 3112, 3175, 3176, 3177, 3178 );
if ( in_array ($id, $ids_to_remove ) )
{
// usuń element $xml->entry[$i]
unset( $xml -> entry[$i] );
}
}
$xml->asXml('feed-main.xml');
echo '<p>wygenerowano https://cdn.projectpro.pl/innsi.pl/feed-main.xml</p>';
// Wczytanie zawartości XML
$file = file_get_contents('https://sklep373302.shoparena.pl/console/integration/execute/name/GoogleProductSearch/lang/pl_PL');
if ($file === false) {
die('Nie udało się pobrać pliku XML.');
}
$xml = new SimpleXMLElement($file);
if ($xml === false) {
die('Nie udało się sparsować pliku XML.');
}
// Namespace dla Google Merchant Center
$namespaces = $xml->getNamespaces(true);
// Tworzenie nowego XML z produktami na promocji
$promocje_xml = new SimpleXMLElement('<rss></rss>');
$promocje_xml->addAttribute('version', '2.0');
$channel = $promocje_xml->addChild('channel');
$channel->addChild('title', (string)$xml->title);
$channel->addChild('link', (string)$xml->link['href']);
$channel->addChild('description', 'Promocje - Innsi');
// Iteracja przez produkty i sprawdzenie, czy posiadają g:sale_price
foreach ($xml->entry as $item) {
$item_ns = $item->children($namespaces['g']);
if (isset($item_ns->sale_price)) {
$new_item = $channel->addChild('item');
// Dodaj tylko g:id, title i g:custom_label_1
if (isset($item_ns->id)) {
$new_item->addChild('g:id', (string)$item_ns->id, $namespaces['g']);
}
if (isset($item->title)) {
$new_item->addChild('title', (string)$item->title);
}
$new_item->addChild('g:custom_label_1', 'promocje', $namespaces['g']);
}
}
// Zapisanie nowego XML do pliku
$promocje_xml->asXml('promocje.xml');
echo '<p>wygenerowano <a href="https://cdn.projectpro.pl/innsi.pl/promocje.xml">https://cdn.projectpro.pl/innsi.pl/promocje.xml</a></p>';
// Wczytaj plik XML
$xmlString = file_get_contents('https://sklep373302.shoparena.pl/console/integration/execute/name/GoogleProductSearch/lang/pl_PL');
$xml = new SimpleXMLElement( $xmlString );
// Nazwy elementów do usunięcia
$elementsToRemove = [
'description',
'g:condition',
'g:availability',
'g:price',
'g:sale_price',
'g:sale_price_effective_date',
'g:shipping_weight',
'g:shipping',
];
// Namespace dla elementów 'g'
$namespaces = $xml->getNamespaces(true);
$gNamespace = $namespaces['g'];
// Pobierz wszystkie elementy entry
foreach ($xml->entry as $entry)
{
foreach ($elementsToRemove as $elementName)
{
$elementParts = explode(':', $elementName);
if (count($elementParts) == 2)
{
$prefix = $elementParts[0];
$localName = $elementParts[1];
$elements = $entry->children($namespaces[$prefix])->$localName;
foreach ($elements as $element)
{
// Usuń element z przestrzenią nazw
$domElement = dom_import_simplexml($element);
$domElement->parentNode->removeChild($domElement);
}
}
else
{
// Usuń element bez przestrzeni nazw
$elements = $entry->$elementName;
foreach ($elements as $element)
{
$domElement = dom_import_simplexml($element);
$domElement->parentNode->removeChild($domElement);
}
}
}
}
// Zapisz zmodyfikowany XML do nowego pliku
$xml->asXML( 'feed-zaufane.xml' );
echo '<p>wygenerowano https://cdn.projectpro.pl/innsi.pl/feed-zaufane.xml</p>';