Skip to content

Instantly share code, notes, and snippets.

@chaddupuis
Created December 21, 2022 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaddupuis/a262058915d3ae5f9652bd183f739a74 to your computer and use it in GitHub Desktop.
Save chaddupuis/a262058915d3ae5f9652bd183f739a74 to your computer and use it in GitHub Desktop.
Google Merchant CSV Creation from Processwire - For Inclusion in Google Shopping
<?php namespace ProcessWire;
require('/var/www/yourpw.com/wire/core/ProcessWire.php');
$st = new ProcessWire('/var/www/yourpw.com/', 'https://yourpw.com/');
// gmerchant header
//id title description link condition price availability image link gtin mpn brand google product category
//product_to_google is a pw field - if 1 send it to google, if 0 do not send
$myGmerchantProducts = $st->pages->find("template=page-general-product|page-other-product, product_to_google=1, sort=title");
$howMany = 0;
foreach ($myGmerchantProducts as $mgp){
echo $mgp->title . '(' . $mgp->product_gtin . ')' . PHP_EOL;
$howMany++;
}
$myProducts = array();
// open the file for writing
$file = fopen('my-gmerchant-products.csv', 'wb');
// save the column headers
fputcsv($file, array('id', 'title', 'description', 'link', 'condition', 'price', 'availability', 'image link', 'gtin', 'mpn', 'brand', 'google product category'));
foreach ($myGmerchantProducts as $mgp) {
$description = '';
$id = $mgp->id;
$title = $mgp->title;
if ($mgp->template == "page-other-product") {
if ($mgp->product->title != '') {
$desc = $mgp->product_field->title;
$description = substr($desc, 0, strpos($desc, '-'));
} else {
$description = $mgp->product_description;
}
}
if ($mgp->template == "page-general-product") {
if ($mgp->product_google_description != '') {
$description = $mgp->product_google_description;
} else {
$description = $mgp->product_description;
}
}
$link = 'https://yourpw.com' . $mgp->url;
$condition = "new";
$price = $mgp->product_price . " USD";
if ($mgp->product_stock == "1") { $availability = "in stock"; } else { $availability = "out of stock"; }
if ($mgp->product_featured_image->first()->url) {
$imagelink = 'https://yourpw.com' . $mgp->product_featured_image->first()->url;
} elseif ($mgp->product_featured_image->url) {
$imagelink = 'https://yourpw.com' . $mgp->product_featured_image->url;
} else { $imagelink = '';}
$gtin = '';
$gtin = $mgp->product_gtin;
$mpn = '';
if ($mgp->product_brand->title != '') { $brand = $mgp->product_brand->title; } else { $brand = '';}
$googleproductcategory = "525";
// dump all to array
$myProducts[] = array($id,$title,$description,$link,$condition,$price,$availability,$imagelink,$gtin,$mpn,$brand,$googleproductcategory);
}
// save each row of the data
foreach ($myProducts as $prodRow) {
fputcsv($file, $prodRow);
}
// Close the file
fclose($file);
// copy and make it web accessible for google merchant account
copy('/whereis/my-gmerchant-products.csv', '/var/www/mypw.com/my-gmerchant-products.csv');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment