
<?
$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
$label_name = 'g:custom_label_0';

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 );

    $price = (float)number_format( str_replace( ' PLN', '', $xpath -> evaluate('string(g:price)', $item) ), 2, '.', '' );
    if ( $price > 44.99 )
    {
      // add custom label 0
      $item -> appendChild( $doc -> createElementNS('http://base.google.com/ns/1.0', $label_name, 'above_45' ) );
    }
    else
    {
      // add custom label 0
      $item -> appendChild( $doc -> createElementNS('http://base.google.com/ns/1.0', $label_name, 'below_45' ) );
    }

    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>';