Google Merchant CSV Creation from Processwire - For Inclusion in Google Shopping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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