Skip to content

Instantly share code, notes, and snippets.

@chaddupuis
Created December 21, 2022 19:07
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/a223fc70980edda830c89a62861dcd1e to your computer and use it in GitHub Desktop.
Save chaddupuis/a223fc70980edda830c89a62861dcd1e to your computer and use it in GitHub Desktop.
Processwire CLI - Example Processing a CSV to Processwire Fields
<?php namespace ProcessWire;
// This is an example of the transfer of product data (could be anything)
// from a csv to their corresponding fields in a processwire template
// and creating the pages. This uses the processwire feature of instantiating
// an instance from a command line script to access global variables
// (https://processwire.com/blog/posts/multi-instance-pw3/).
require('/var/www/dev.dev.com/wire/core/ProcessWire.php');
$st = new ProcessWire('/var/www/dev.dev.com/', 'https://dev.dev.com/');
// Need Tab Delimited File Because Of Long Descriptions
ini_set('auto_detect_line_endings', TRUE);
$rows = array_map(function($v){return str_getcsv($v, "\t");}, file('mystorev2.csv'));
//$rows = array_map('str_getcsv', file('yyhstorev1.csv'));
$header = array_shift($rows);
$csv = array();
foreach ($rows as $row) {
$csv[] = array_combine($header, $row);
}
// End of Read In
// Start Read In of Merchant CSV
ini_set('auto_detect_line_endings', TRUE);
$grows = array_map(function($v){return str_getcsv($v, "\t");}, file('otherstore.csv'));
//$rows = array_map('str_getcsv', file('yyhstorev1.csv'));
$gheader = array_shift($grows);
$gcsv = array();
foreach ($grows as $grow) {
$gcsv[] = array_combine($gheader, $grow);
}
// End of Read In
// HEADERS in gmerchant
//id title description link condition price availability image link gtin mpn brand google product category
// HEADERS IN Products File
// product_description,product_title,product_brand,product_real_name,product_wan_name,
// post_name,regular_price,sku,stock_status,featured_image,featured_image_name,category
$howMany = count($csv);
foreach ($csv as $d) {
// TO ACTUALLY CREATE if ($writeThis == 1) {$p->save();
$writeThis = 2;
// Formulas
if ($d[category] == 'H Products') {
// SETUP H products
// Create the Files
$p = new Page(); // create new page object
$p->template = 'page-h-product'; // set template
$p->parent = wire('pages')->get('/h-products'); // set the parent
$p->name = $d[post_name]; // give it a name used in the url for the page
$p->title = $d[product_title]; // set page title (not neccessary but recommended)
if ($writeThis == 1) {$p->save();}
$p->product_h_truename = $d[product_real_name];
$p->product_price = $d[regular_price];
$p->product_sku = $d[sku];
// Set gtin if we have it
foreach ($gcsv as $gm) {
$gmtitle = preg_replace('/\s+/', '', $gm[title]);
$gmtitle = strtolower($gmtitle);
$ourtitle = preg_replace('/\s+/', '', $d[product_title]);
$ourtitle = strtolower($ourtitle);
if (($gmtitle == $ourtitle) && ($gm[gtin] != '')) {
$p->product_gtin = $gm[gtin];
echo 'they match - gm ' . $gmtitle . ' ' . $ourtitle . ' gtin is ' . $gm[gtin] . PHP_EOL;
} else { if (($gmtitle == $ourtitle) && ($gm[gtin] = '')) { echo 'NO GTIN FOR ' . $d[product_title] . ' GM IS ' . $gmtitle . ' OUR IS ' . $ourtitle . PHP_EOL;} }
}
/// CHANGE THIS DEPENDENT ON BRAND - OR REWRITE IN REGULAR SCRIPT
if ($d[product_description] != '') {
$p->product_description = $d[product_description]; } else { $p->body = '';}
if ($writeThis == 1) {$p->save();}
// NOW DROP DOWNS
// Category
$typeCat = $st->pages->get("template=main-categories-i, title=$d[category]");
$p->product_type_category = $typeCat->id;
if ($writeThis == 1) {$p->save();}
// Brands
$typeBrand = $st->pages->get("template=main-brands-i, title=$d[product_brand]");
$p->product_brand = $typeBrand->id;
if ($writeThis == 1) {$p->save();}
// Format
if ($d[product_brand] == 'A'){ $p->product_h_formatdescribe = 1;}
if ($d[product_brand] == 'B'){ $p->product_h_formatdescribe = 2;}
if ($d[product_brand] == 'C'){ $p->product_h_formatdescribe = 3;}
if ($d[product_brand] == 'D'){ $p->product_h_formatdescribe = 4;}
if ($d[product_brand] == 'E'){ $p->product_h_formatdescribe = 5;}
if ($d[product_brand] == 'F'){ $p->product_h_formatdescribe = 6;}
if ($d[product_brand] == 'G'){ $p->product_h_formatdescribe = 7;}
// Related Formulas
$relFormula = $st->pages->get("template=category-formulas-ind, title=$d[product_wan_name]");
if ($relFormula != '') { $p->category_relationships_formulas = $relFormula->id; }
if ($writeThis == 1) {$p->save();}
// Stock + Google Shopping
$p->product_stock = true;
if ($p->product_gtin != '') { $p->product_to_google = true; } else {$p->product_to_google = false; }
// Save
if ($writeThis == 1) {$p->save();}
// Featured Image
if ($d[featured_image] != '') {
$p->product_featured_image = $d[featured_image];
if ($writeThis == 1) {$p->save();}
}
// final save to get it to get remote images
if ($writeThis == 1) {$p->save();}
// END Herbal Formulas
} else {
// SETUP EVERYTHING ELSE
// Create the Files
$p = new Page(); // create new page object
$p->template = 'page-general-product'; // set template
$p->parent = wire('pages')->get('/h-products'); // set the parent
$p->name = $d[post_name]; // give it a name used in the url for the page
$p->title = $d[product_title]; // set page title (not neccessary but recommended)
if ($writeThis == 1) {$p->save();}
$p->product_price = $d[regular_price];
$p->product_sku = $d[sku];
// Set gtin if we have it
foreach ($gcsv as $gm) {
$gmtitle = preg_replace('/\s+/', '', $gm[title]);
$gmtitle = strtolower($gmtitle);
$ourtitle = preg_replace('/\s+/', '', $d[product_title]);
$ourtitle = strtolower($ourtitle);
if (($gmtitle == $ourtitle) && ($gm[gtin] != '')) {
$p->product_gtin = $gm[gtin];
echo 'they match - gm ' . $gmtitle . ' ' . $ourtitle . ' gtin is ' . $gm[gtin] . PHP_EOL;
} else { if (($gmtitle == $ourtitle) && ($gm[gtin] = '')) { echo 'NO GTIN FOR ' . $d[product_title] . ' GM IS ' . $gmtitle . ' OUR IS ' . $ourtitle . PHP_EOL;} }
}
/// CHANGE THIS DEPENDENT ON BRAND - OR REWRITE IN REGULAR SCRIPT
if ($d[product_description] != '') {
$p->product_description = $d[product_description]; } else { $p->body = '';}
if ($writeThis == 1) {$p->save();}
// NOW DROP DOWNS
// Category
$typeCat = $st->pages->get("template=main-categories-ind, title=$d[category]");
$p->product_type_category = $typeCat->id;
if ($writeThis == 1) {$p->save();}
// Brands
if ($d[product_brand] != '') {
$typeBrand = $st->pages->get("template=main-brands-ind, title=$d[product_brand]");
$p->product_brand = $typeBrand->id;
if ($writeThis == 1) {$p->save();}
}
// Stock + Google Shopping
$p->product_stock = true;
if ($p->product_gtin != '') { $p->product_to_google = true; } else {$p->product_to_google = false; }
// Save
if ($writeThis == 1) {$p->save();}
// Featured Image
if ($d[featured_image] != '') {
$p->product_featured_image = $d[featured_image];
if ($writeThis == 1) {$p->save();}
}
// final save to get it to get remote images
if ($writeThis == 1) {$p->save();}
// END ALL THE REST
}
} // END FOREACH CSV LOOP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment