Skip to content

Instantly share code, notes, and snippets.

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 ArnoutPullen/12ccfdb6aa92af6cd4f4bfba403d5ae6 to your computer and use it in GitHub Desktop.
Save ArnoutPullen/12ccfdb6aa92af6cd4f4bfba403d5ae6 to your computer and use it in GitHub Desktop.
WordPress: Woocommerce Product Attributes - Bulk Modifier ( from custom to taxonomy attributes)
<?php
/**
* Plugin Name: Product Attributes - Bulk Modifier
* Description: Bulk update 'custom product attributes' to 'taxonomy product attributes'. Supports the GET variables, like: wpq_mode=run&wpq_from=colour&wpq_to=pa_colour&wpq_ppp=10&wpq_offset=0&wpq_post_type=product&wpq_post_status=any. WARNING: Backup DB first!!!
* Plugin Author: birgire
* Plugin URI: https://gist.github.com/birgire/0ed300ae4436fcaf508c
* Version: 0.0.2
*/
/**
* Example:
* To change all custom product attributes "colour" to "pa_colour" products taxonomy, we can run:
*
* http://example.tld/?wpq_mode=run&wpq_from=colour&wpq_to=pa_colour&wpq_ppp=100&wpq_offset=0&wpq_post_type=product&wpq_post_status=any
*
* WARNING: Remember to backup your database first!!!
*/
add_action( 'template_redirect', function()
{
// Activate product attribute modification (only available for admins):
if( current_user_can( 'manage_options' ) && 'run' === filter_input( INPUT_GET, 'wpq_mode' ) )
{
// Setup:
$meta_key = '_product_attributes';
// User input:
$from = filter_input( INPUT_GET, 'wpq_from', FILTER_SANITIZE_STRING );
$to = filter_input( INPUT_GET, 'wpq_to', FILTER_SANITIZE_STRING );
$post_type = filter_input( INPUT_GET, 'wpq_post_type', FILTER_SANITIZE_STRING );
$post_status = filter_input( INPUT_GET, 'wpq_post_status', FILTER_SANITIZE_STRING );
$ppp = filter_input( INPUT_GET, 'wpq_ppp', FILTER_SANITIZE_NUMBER_INT );
$offset = filter_input( INPUT_GET, 'wpq_offset', FILTER_SANITIZE_NUMBER_INT );
// Default values:
if( empty( $ppp ) )
{
$ppp = 10;
}
if( empty( $offset ) )
{
$offset = 0;
}
if( empty( $post_type ) )
{
$post_type = 'product';
}
if( empty( $post_status ) )
{
$post_status = 'any';
}
if( empty( $from ) || empty( $to ) )
{
wp_die( 'Oh, rembember that the "from" and "to" variable must be set!');
}
// Fetch products with product attributes:
$args = array(
'post_type' => sanitize_key( $post_type ),
'fields' => 'ids',
'posts_per_page' => (int) $ppp,
'offset' => (int) $offset,
'meta_key' => $meta_key,
'post_status' => sanitize_key( $post_status ),
);
$post_ids = get_posts( $args );
foreach( (array) $post_ids as $post_id )
{
$meta = get_post_meta( $post_id, $meta_key, true );
$product_needs_update = false;
foreach( (array) $meta as $key => $terms )
{
// Locate our meta attribute:
if( $from === $terms['name'] )
{
$product_needs_update = true;
$tmp = explode( '|', $terms['value'] );
$product_terms = array();
foreach( (array) $tmp as $term )
{
$product_terms[] = sanitize_key( $term );
}
// Remove the product meta attribute:
unset( $meta[$from] );
// Add it again as product taxonomy attribute:
$meta["{$to}"] = array(
'name' => "{$to}",
'value' => '',
'position' => $terms['position'],
'is_visible' => $terms['is_visible'],
'is_variation' => $terms['is_variation'],
'is_taxonomy' => 1,
);
} // end if
} // end foreach
if( $product_needs_update )
{
// Assign terms to the post (create them if they don't exists)
$term_taxonomy_ids = wp_set_object_terms( $post_id, $product_terms, $to, false );
if ( is_wp_error( $term_taxonomy_ids ) )
{
printf( "There was an error somewhere and the terms couldn't be set for post_id: %d <hr/>", $post_id );
}
else
{
update_post_meta( $post_id, $meta_key, $meta );
printf( "Success! The taxonomy post attributes were set for post_id: %d <hr/>", $post_id );
} // end if
} // end if current post needs update
} // end foreach post
die( 'Done!');
} // end if "run" action
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment