Add custom labels based on price thresholds in XML processing

This commit is contained in:
2024-11-08 21:41:39 +01:00
parent e8eb8f7fb3
commit d065c6ebd7

View File

@@ -10,6 +10,7 @@ $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 )
{
@@ -21,11 +22,23 @@ foreach ( $items as $item )
}
// $google_category = $xpath->evaluate('string(g:google_product_category)', $item);
$brand = $xpath->evaluate('string(g:brand)', $item);
$quantity = $xpath->evaluate('string(quantity)', $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' );
@@ -40,122 +53,3 @@ foreach ( $items as $item )
$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>';