Generating XML file for Google Merchant

Generating XML file for Google Merchant

An example of generating an XML feed for Google Merchant in PHP with a minimum set of required fields.

Format description: https://support.google.com/merchants/answer/7052112

Merchant.php file code

<?php
// Connect to DB
$dbh = new PDO('mysql:dbname=db_name;host=localhost', 'ЛОГИН', 'ПАРОЛЬ');
 
$out = '<?xml version="1.0" encoding="utf-8"?>' . "\r\n";
$out .= '<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">' . "\r\n";
$out .= '<channel>' . "\r\n";
 
// Name of shop
$out .= '<title>Horns and hooves</title>' . "\r\n";
 
// Shop description
$out .= '<description>Shop selling ...</description>' . "\r\n";

// URL of the main page of the store
$out .= '<link>https://example.com/</link>' . "\r\n";

// Displaying products
$sth = $dbh->prepare("SELECT * FROM `prods`");
$sth->execute();
$prods = $sth->fetchAll(PDO::FETCH_ASSOC);

foreach ($prods as $row) {
	$out .= '<item>' . "\r\n";

	// Product ID
	$out .= '<g:id>' . $row['id'] . '</g:id>' . "\r\n";

	// Product Name
	$out .= '<title>' . $row['name'] . '</title>' . "\r\n";
	
	// URL of the product page on the store website
	$out .= '<link>https://example.com/prod/' . $row['id'] . '.html</link>' . "\r\n";

	// Product description
	$out .= '<g:description><![CDATA['. htmlspecialchars($row['text']) . ']]></g:description>' . "\r\n";    
	
	// Price
	$out .= '<g:price>' . $row['price'] . '.00 USD</g:price>' . "\r\n";
	
	// Discount price
	$out .= '<g:sale_price>' . $row['price'] . ' . 00 USD</g:sale_price>' . "\r\n";
	
	// Product images
	$out .= '<g:image_link>https://example.com/img/1.jpg</g:image_link>' . "\r\n";
	$out .= '<g:image_link>https://example.com/img/2.jpg</g:image_link>' . "\r\n";
	
	// Manufacturer
	$out .= '<g:brand>' . $row['brand'] . '</g:brand>' . "\r\n";
	
	// Availability
	// in stock, out of stock, preorder, backorder
	$out .= '<g:availability>in stock</g:availability>' . "\r\n";
	
	// Product category
	// https://www.google.com/basepages/producttype/taxonomy-with-ids.en-US.txt
	$out .= '<g:google_product_category>Furniture > Sofas</g:google_product_category>' . "\r\n";
	
	// Cost and delivery time
	$out .= '
	<g:shipping>
		<g:country>EN</g:country>
		<g:price>3.00 USD</g:price>
		<g:min_handling_time>1</g:min_handling_time>
		<g:max_handling_time>3</g:max_handling_time>	
		<g:min_transit_time>1</g:min_transit_time>
		<g:max_transit_time>3</g:max_transit_time>
	</g:shipping>';
   
	$out .= '</item>' . "\r\n";
}
 
$out .= '</channel>' . "\r\n";

// Output to browser
header('Content-Type: text/xml; charset=utf-8');
echo $out;

// Or save to file
// file_put_contents(__DIR__ . '/merchant.xml', $out);

exit();
PHP

An example of the generated merchant.xml file:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
	<titleHorns and hooves</title>
	<description>Shop selling ...</description>
	<link>https://example.com/</link>
	<item>
		<g:id>123</g:id>
		<title>Venis Deli Ash 45x120 tile (Ceramic, wall)</title>
		<link>https://example.com/prod/123</link>
		<g:description>
			<![CDATA[Collection: Deli, Width: 45cm, Length: 120cm]]>
		</g:description>
		<g:price>672.00 USD</g:price>
		<g:sale_price>483.00 USD</g:sale_price>
		<g:image_link>https://example.com/img/1.jpg</g:image_link>
		<g:image_link>https://example.com/img/2.jpg</g:image_link>
		<g:brand>Venis</g:brand>
		<g:availability>in stock</g:availability>
		<g:google_product_category>Furniture > Sofas</g:google_product_category>
		<g:shipping>
			<g:country>EN</g:country>
			<g:price>3.00 USD</g:price>
			<g:min_handling_time>1</g:min_handling_time>
			<g:max_handling_time>3</g:max_handling_time>
			<g:min_transit_time>1</g:min_transit_time>
			<g:max_transit_time>3</g:max_transit_time>
		</g:shipping>
	</item>
</channel>
</rss>

Redirecting from .php to .xml

For example, this script lies in the root of a site named sitemap.php, you want it to open at https://example.com/sitemap.xml. In the .htaccess file, you need to add an entry:

RewriteEngine On
RewriteRule ^merchant.xml$ merchant.php [L]
htaccess
04.06.2021
95

Comments

to add a comment.

Other publications

Regular Expression Shortcodes
Often on sites, it becomes necessary to insert dynamic information blocks into the texts of pages - banners, phones...
25
0
Checking data with regular expressions
A collection of regular expressions with examples in PHP for validating data from form fields.
49
0
Automatic setting of the user's time zone
date_default_timezone_set sets the default timezone used by all date / time functions.
113
0
Serialize function, possible problems
The serialize () function returns a string representation of any value (array, object, etc.). Using serialize on an...
32
0
How to set up Last-Modified
The Last-Modified header helps to optimize the loading of web pages and make the job easier for search engines.
100
0
How to find out if a request came via AJAX
To distinguish between direct and AJAX requests, simply check the value of the $ _SERVER global variable
60
0