Skip to content

Instantly share code, notes, and snippets.

@rxnlabs
Last active February 28, 2018 15:20
Show Gist options
  • Save rxnlabs/a6e7baa3d2f9050ea0fb to your computer and use it in GitHub Desktop.
Save rxnlabs/a6e7baa3d2f9050ea0fb to your computer and use it in GitHub Desktop.
WP - PHP Convert/Update/Migrate WooCommerce data attributes to a new custom post type. Convert WooCommerce products to new custom post type. Should be used from the Command Line. This was used to convert a WooCommerce store to another post type when WooCommerce was no longer needed.
<?php
ini_set('memory_limit','1500M');
/**
* Update/Covert all WooCommerce Products data to a new post type
*
* Moving all WooCommerce custom data to a new custom post type. Should only be executed from CLI since it uses so many processes.
*
* @author De'Yonte W. <admin@rxnlabs.com>
* @return void
*/
function migrate_woocommerce(){
global $wpdb;
$query = 'SELECT * FROM '.$wpdb->prefix.'woocommerce_attribute_taxonomies';
$products = $wpdb->get_results($query);
// new post type and taxonomy to convert to
$new_taxonomy = 'product-attribute';
$new_post_type = 'products';
// old post type and taxonomy to convert from. WooCoomerce post type
$woocommerce_post_type = 'product';
$woocommerce_taxonomy_prefix = 'pa_';
// get all the parent "categories"/attributes so we can make the the taxonomy values the children of the parent "categories"/attributes
$terms = array();
foreach($products as $att){
$label = $att->attribute_label;
if( !term_exists( $label, $new_taxonomy ) && !empty($label) ){
$term = wp_insert_term($label,$new_taxonomy,array(
'slug'=>$att->attribute_name
));
$term = json_decode(json_encode($term), FALSE);
}elseif( !empty($label) ){
$term = get_term_by('name',$label,$new_taxonomy);
}
$terms[] = array('id'=>intval($term->term_id),'name'=>$att->attribute_name,'term_taxonomy_id'=>intval($term->term_taxonomy_id));
}
if( !empty($terms) ){
foreach($terms as $term){
// loop through all terms in the database that are related to the custom taxonomy created by WooCommerce
$old_terms = get_terms($woocommerce_taxonomy_prefix.$term['name'],array(
'fields'=>'all'
));
if( !empty($old_terms) ){
foreach($old_terms as $old_term){
// make the WooCommerce "product attributes" children of the new related "product attributes" (e.g. Move all WooCommerce product attributes to the new custom taxonomy that's related to the new Custom Post Type)
if( !term_exists( $old_term->name, $new_taxonomy ) ){
$new_term = wp_insert_term($old_term->name,$new_taxonomy,array(
'parent'=>$term['id'],
'slug'=>$old_term->slug,
'description'=>$old_term->description
));
if( !($new_term instanceof WP_Error) ){
// get the posts that have the old term and attach the new taxonomy to that post
$post_with_terms = get_posts(array(
'post_type'=>$woocommerce_post_type,
'numberposts'=>-1,
$woocommerce_taxonomy_prefix.$term['name']=>$old_term->name
));
}
if( !($new_term instanceof WP_Error) && !empty($post_with_terms) ){
// grab the terms taxonomy ID to programatically update the term count later since 'wp_set_post_terms' does not update the term count
$terms_taxonomy[] = $new_term['term_taxonomy_id'];
foreach( $post_with_terms as $current_post ){
$set_term = wp_set_post_terms( $current_post->ID, $new_term['term_id'], $new_taxonomy, true );
if( is_array($set_term) )
echo "new term created\n";
elseif( is_bool($set_term) )
echo "no post found to attach term to\n";
elseif( $set_term instanceof WP_Error )
echo "invalid taxonomy. Taxonomy ID not found\n";
else
echo "wrong term id\n";
}
}
wp_reset_postdata();
}
}
}
}
// update the term count
if( !empty($terms_taxonomy) ){
$wpdb->show_errors();
foreach( $terms_taxonomy as $taxonomy ){
$term_count = $wpdb->get_row('SELECT COUNT(*) AS count FROM '.$wpdb->term_relationships.' WHERE term_taxonomy_id = '.$taxonomy);
$products = $wpdb->update( $wpdb->term_taxonomy, array('count'=>intval($term_count->count)), array('term_taxonomy_id'=>intval($taxonomy)) );
}
}
// update the post types of WooCommerce products to new post type
$woocommerceposts = get_posts(array(
'post_type'=>$woocommerce_post_type,
'numberposts'=>-1,
'post_status'=>'any'
));
if( !empty($woocommerceposts) ){
foreach( $woocommerceposts as $woo ){
$updated = set_post_type( $woo->ID, $new_post_type );
if( $updated === 0 )
echo "not converted\n";
elseif( $updated === 1 )
echo "converted\n";
else
echo "I have no idea what happened\n";
}
}
}
echo "script completed\n";
}
// change the DB_HOST constant to the localhost IP address so it works on a Mac. "Localhost" means something different in a Mac environment
define('DB_HOST','127.0.0.1');
// load the wp-load.php file to use WordPress functions. File path is assumming this is being run from the current theme folder (any theme folder would work)
require __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'wp-load.php';
// check if PHP is being executed from the command line
if( php_sapi_name() === 'cli' )
migrate_woocommerce();
else
echo "Warning: Do Not Do This In The Browser";
#command to execute script from Command Line. Assuming PHP is in global PATH for your OS
php name-of-php-file.php
#if PHP is not in glbal PATH for your OS
./path-to-php-executable-file/php name-of-php-file.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment