Skip to content

Instantly share code, notes, and snippets.

@chaddupuis
Created December 21, 2022 18:44
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/c97832c0f5789902f6486f246844d5ea to your computer and use it in GitHub Desktop.
Save chaddupuis/c97832c0f5789902f6486f246844d5ea to your computer and use it in GitHub Desktop.
Woocommerce WP REST Api Transfer to Processwire Site
<?php namespace ProcessWire;
// Using the Woocommerce/WP REST API and bootstrapping a processwire site from the script,
// build a csv (to be imported into a processwire link rewrite/redirect module) to ensure
// no dead links after site migrations from woocommerce to processwire.
require('/yourpwinstall/wire/core/ProcessWire.php');
$st = new ProcessWire('/your/site/location/', 'https://yoururl.yours');
// Install:
// composer require automattic/woocommerce
// Setup:
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://', // Your store URL
'', // Your consumer key
'', // Your consumer secret
[
'wp_api' => true, // Enable the WP REST API integration
'version' => 'wc/v2' // WooCommerce WP REST API version
]
);
// Get all products
$allstproducts100 = $woocommerce->get('products',['page' => 1, 'per_page' => 100]);
$allstproducts200 = $woocommerce->get('products',['page' => 2, 'per_page' => 100]);
$allstproducts300 = $woocommerce->get('products',['page' => 3, 'per_page' => 100]);
$allstproducts = array_merge($allstproducts100,$allstproducts200,$allstproducts300);
$fp = fopen('mylinks.csv', 'wa+');
$mymatches = 0;
$myprodmatches = 0;
$mynots = 0;
$myprodnots = 0;
$myLinks = array();
foreach($allstproducts as $product) {
// Find A Match
$cleanName = str_replace(',','',$product->name);
foreach ($product->categories as $name) {
$woolinkone = '';
$woolinktwo = '';
if ($name->name == 'Category You Want') {
$allformulas = $st->pages->get("template=your-template, title=$cleanName");
if ($allformulas != '') {
$woolinkone = str_replace("https://your-site.com","",$product->permalink);
$woolinktwo = str_replace("https://your-site.com/shop","",$product->permalink);
$myLinks[] = array($woolinkone,$allformulas->url);
$myLinks[] = array($woolinktwo,$allformulas->url);
echo $product->name . ' formula matched ' . $allformulas->url . PHP_EOL;
$mymatches++;
} else { // echo $product->name . ' did not match ' . PHP_EOL;
$mynots++;
}
}
}
$allproducts = $st->pages->get("template=other-template, title=$cleanName");
$woolinkthree = '';
$woolinkfour = '';
if ($allproducts != '') {
$woolinkthree = str_replace("https://your-site.com","",$product->permalink);
$woolinkfour = str_replace("https://your-site.com/shop","",$product->permalink);
$myLinks[] = array($woolinkthree,$allproducts->url);
$myLinks[] = array($woolinkfour,$allproducts->url);
echo $product->name . ' product matched ' . $allproducts->url . PHP_EOL;
$myprodmatches++;
} else { // echo $product->name . ' product did not match ' . PHP_EOL;
$myprodnots++;
}
}
echo 'I Have ' . $mymatches . ' Matches - prod ' . $myprodmatches . PHP_EOL;
echo 'and ' . $mynots . ' nots - prod' . $myprodnots . PHP_EOL;
foreach($myLinks as $ml) {
fputcsv($fp, $ml);
}
fclose($fp);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment