
<?
$file = file_get_contents('https://sklepagm.pl/modules/xmlfeeds/xml_files/feed_7.xml');
$doc = new DOMDocument();
$doc->loadXML($file);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('g', 'http://base.google.com/ns/1.0');

$items = $xpath->query('//channel/item');
$channel = $xpath->query('//channel')->item(0);  // Znajdź element channel, aby usunąć elementy item

$itemCount = 0;  // Inicjalizuj licznik elementów item

foreach ( $items as $item )
{
    $itemCount++;  // Zwiększ licznik elementów item

    if ( $itemCount > 999999 ) {
      $channel -> removeChild( $item );
      continue;
    }

    // $google_category = $xpath->evaluate('string(g:google_product_category)', $item);
    $brand = $xpath->evaluate('string(g:brand)', $item);
    $quantity = $xpath->evaluate('string(quantity)', $item);
    $category_tree = strtolower( $xpath -> evaluate( 'string(product_category_tree)', $item ) );
    $category_tree = explode( '|', $category_tree );

    if ( $quantity > 0 )
    {
      $availability = $doc->createElementNS('http://base.google.com/ns/1.0', 'g:availability', 'in stock' );
      $item -> appendChild( $availability );
    }
    else
    {
      $availability = $doc->createElementNS('http://base.google.com/ns/1.0', 'g:availability', 'out of stock' );
      $item -> appendChild( $availability );
    }
}

$doc -> save( 'main-feed.xml' );
echo '<p>Wygenerowano https://cdn.projectpro.pl/sklepagm.pl/main-feed.xml</p>';

$file = file_get_contents('https://sklepagm.pl/modules/xmlfeeds/xml_files/feed_9.xml');
$xml = new SimpleXMLElement($file);

foreach ($xml->xpath('//item/*[
    not(
           name() = "g:id"
        or name() = "g:title"
    )
]') as $child) unset($child[0]);

$label_name = 'g:custom_label_0';

for ( $i = 0; $i < count( $xml -> channel -> item ); $i++ )
{
  $xml -> channel -> item[$i] -> $label_name = 'kawa';
}
$xml -> asXml( 'kawa.xml' );
echo '<p>Wygenerowano https://cdn.projectpro.pl/sklepagm.pl/kawa.xml</p>';

// ekspresy do domu
$file = file_get_contents('https://sklepagm.pl/modules/xmlfeeds/xml_files/feed_8.xml');
$xml = new SimpleXMLElement($file);

foreach ($xml->xpath('//item/*[
    not(
           name() = "g:id"
        or name() = "g:title"
    )
]') as $child) unset($child[0]);

$label_name = 'g:custom_label_0';

for ( $i = 0; $i < count( $xml -> channel -> item ); $i++ )
{
  $xml -> channel -> item[$i] -> $label_name = 'ekspresy-do-domu';
}
$xml -> asXml( 'ekspresy-do-domu.xml' );
echo '<p>Wygenerowano https://cdn.projectpro.pl/sklepagm.pl/ekspresy-do-domu.xml</p>';

// akcesoria
$file = file_get_contents('https://sklepagm.pl/modules/xmlfeeds/xml_files/feed_10.xml');
$xml = new SimpleXMLElement($file);

foreach ($xml->xpath('//item/*[
    not(
           name() = "g:id"
        or name() = "g:title"
    )
]') as $child) unset($child[0]);

$label_name = 'g:custom_label_0';

for ( $i = 0; $i < count( $xml -> channel -> item ); $i++ )
{
  $xml -> channel -> item[$i] -> $label_name = 'akcesoria';
}
$xml -> asXml( 'akcesoria.xml' );
echo '<p>Wygenerowano https://cdn.projectpro.pl/sklepagm.pl/akcesoria.xml</p>';

// promocje
$file = file_get_contents('https://sklepagm.pl/modules/xmlfeeds/xml_files/feed_11.xml');
$xml = new SimpleXMLElement($file);

foreach ($xml->xpath('//item/*[
    not(
           name() = "g:id"
        or name() = "g:title"
    )
]') as $child) unset($child[0]);

$label_name = 'g:custom_label_0';

for ( $i = 0; $i < count( $xml -> channel -> item ); $i++ )
{
  $xml -> channel -> item[$i] -> $label_name = 'promocje';
}
$xml -> asXml( 'promocje.xml' );
echo '<p>Wygenerowano https://cdn.projectpro.pl/sklepagm.pl/promocje.xml</p>';

// zdrowie
$file = file_get_contents('https://sklepagm.pl/modules/xmlfeeds/xml_files/feed_12.xml');
$xml = new SimpleXMLElement($file);

foreach ($xml->xpath('//item/*[
    not(
           name() = "g:id"
        or name() = "g:title"
    )
]') as $child) unset($child[0]);

$label_name = 'g:custom_label_0';

for ( $i = 0; $i < count( $xml -> channel -> item ); $i++ )
{
  $xml -> channel -> item[$i] -> $label_name = 'zdrowie';
}
$xml -> asXml( 'zdrowie.xml' );
echo '<p>Wygenerowano https://cdn.projectpro.pl/sklepagm.pl/zdrowie.xml</p>';

// dom i ogród
$file = file_get_contents('https://sklepagm.pl/modules/xmlfeeds/xml_files/feed_13.xml');
$xml = new SimpleXMLElement($file);

foreach ($xml->xpath('//item/*[
    not(
           name() = "g:id"
        or name() = "g:title"
    )
]') as $child) unset($child[0]);

$label_name = 'g:custom_label_0';

for ( $i = 0; $i < count( $xml -> channel -> item ); $i++ )
{
  $xml -> channel -> item[$i] -> $label_name = 'dom-i-ogrod';
}
$xml -> asXml( 'dom-i-ogrod.xml' );
echo '<p>Wygenerowano https://cdn.projectpro.pl/sklepagm.pl/dom-i-ogrod.xml</p>';