Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress: WooCommerce Product Attributes - Bulk Modifier ( from custom meta attributes to taxonomy attributes)
<?php
/**
* Plugin Name: WooPAM: Woo Product Attributes Modifier
* Description: Bulk update 'custom meta product attributes' to 'taxonomy product attributes' in WooCommerce. Supports the GET variables, like: woopam_mode=run&woopam_from_attribute_meta=colour&woopam_to_attribute_tax=pa_colour&woopam_keep_attribute_meta&woopam_posts_per_page=10&woopam_paged=0&woopam_post_type=product&woopam_post_status=any. WARNING: Backup DB first!!!
* Plugin Author: birgire
* Author URI: https://github.com/birgire
* Plugin URI: https://gist.github.com/birgire/0ed300ae4436fcaf508c
* Version: 1.0.0
* License: GPL2+
* Text Domain: woopam
* Requires PHP: 5.4
*/
/*
*---------
* Support:
*---------
* If this helped your project and saved you some time, you can support the developement here:
*
* https://www.buymeacoffee.com/birgire
*
* Thanks.
*
*---------------
* Usage Example:
*---------------
*
* To change all custom product meta attributes "color" to "pa_color" products taxonomy (where pa_ is a WooCommerce auto-added prefix), we can run:
*
* https://example.com/?woopam_mode=run&woopam_from_attribute_meta=color&woopam_to_attribute_tax=pa_color&woopam_keep_attribute_meta=1&woopam_posts_per_page=100&woopam_paged=0&woopam_post_type=product&woopam_post_status=any
*
* REMEMBER: Create the product taxonomy beforehand here (without pa_ prefixing it yourself):
*
* https://example.com/wp-admin/edit.php?post_type=product&page=product_attributes
*
* WARNING: Remember to backup your database first!!!
*
*-----------
* CHANGELOG:
*-----------
*
* 2021-12-13 v1.0.0 Overhaul (Work: 5 hours)
*
*------
* TODO:
*------
*
* - Split into more smaller functions.
* - Add tests.
* - Add more documentation.
* - Look into UI.
*/
add_action( 'template_redirect', function() {
// Activate product attribute modification (only available for admins).
if ( ! current_user_can( 'manage_options' ) || 'run' !== filter_input( INPUT_GET, 'woopam_mode' ) ) {
return;
}
// User input.
$keep_attribute_meta = filter_input( INPUT_GET, 'woopam_keep_attribute_meta', FILTER_VALIDATE_BOOLEAN );
$from_attribute_meta = filter_input( INPUT_GET, 'woopam_from_attribute_meta', FILTER_SANITIZE_STRING );
$to_attribute_tax = filter_input( INPUT_GET, 'woopam_to_attribute_tax', FILTER_SANITIZE_STRING );
$post_type = filter_input( INPUT_GET, 'woopam_post_type', FILTER_SANITIZE_STRING );
$post_status = filter_input( INPUT_GET, 'woopam_post_status', FILTER_SANITIZE_STRING );
$posts_per_page = filter_input( INPUT_GET, 'woopam_posts_per_page', FILTER_SANITIZE_NUMBER_INT );
$paged = filter_input( INPUT_GET, 'woopam_paged', FILTER_SANITIZE_NUMBER_INT );
// Default values.
if ( empty( $posts_per_page ) ) {
$posts_per_page = 10;
}
if ( empty( $paged ) ) {
$paged = 1;
}
if ( empty( $post_type ) ) {
$post_type = 'product';
}
if ( empty( $keep_attribute_meta ) ) {
$keep_attribute_meta = false;
}
if ( empty( $post_status ) ) {
$post_status = 'any';
}
if ( empty( $from_attribute_meta ) || empty( $to_attribute_tax ) ) {
wp_die( esc_html__( 'Oh, rembember that the "from_attribute_meta" and "to_attribute_tax" variable must be set!', 'woopam' ) );
}
$meta_key = '_product_attributes';
// Fetch products with product attributes:
$args = array(
'post_type' => sanitize_key( $post_type ),
'fields' => 'ids',
'posts_per_page' => (int) $posts_per_page,
'paged' => absint ( $paged ),
'meta_key' => $meta_key,
'post_status' => sanitize_key( $post_status ),
);
$post_ids = get_posts( $args );
$total = 0;
$total_modified = 0;
$msg = esc_html__( "Bulk update 'custom meta product attributes' to 'taxonomy product attributes'", 'woopam' );
printf(
'<h1>%s</h1>%s<ul>',
$msg,
esc_html__( 'START', 'woopam' )
);
foreach ( (array) $post_ids as $post_id ) {
$total++;
$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_attribute_meta === $terms['name'] ) {
$product_needs_update = true;
$tmp = explode( '|', $terms['value'] );
$product_terms = array();
foreach ( (array) $tmp as $term ) {
$product_terms[] = $term;
}
// Remove the product meta attribute:
if ( ! $keep_attribute_meta ) {
unset( $meta[$key] );
}
// Add it again as product taxonomy attribute:
$meta["{$to_attribute_tax}"] = array(
'name' => "{$to_attribute_tax}",
'value' => '',
'position' => $terms['position'],
'is_visible' => $terms['is_visible'],
'is_variation' => $terms['is_variation'],
'is_taxonomy' => 1,
);
} // end if
} // end foreach
echo '<li>';
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_attribute_tax, false );
if ( is_wp_error( $term_taxonomy_ids ) ) {
$msg = sprintf(
esc_html__( 'Error! Terms couldn\'t be set for product id: %d and WP error description: "%s" and product taxonomy slug: "%s"', 'woopam' ),
(int) $post_id,
esc_html( $term_taxonomy_ids->get_error_code() ),
esc_html( $to_attribute_tax )
);
printf( '<strong>%s</strong><br/>', $msg );
} else {
update_post_meta( $post_id, $meta_key, $meta );
$total_modified++;
$msg = sprintf(
esc_html__( 'Success! The taxonomy post attributes were set for product id: %d', 'woopam' ),
(int) $post_id
);
printf( '<strong>%s</strong><br/>', $msg );
}
} else {
$msg = sprintf(
esc_html__( 'Nothing to do here! No product attributes were set for product id: %d', 'woopam' ),
$post_id
);
printf( '%s<br/>', $msg );
}
echo '</li>';
} // end foreach post.
echo '</ul><br/>';
printf(
esc_html__( 'Total products checked: %d', 'woopam' ),
(int) $total
);
echo '<br/>';
printf(
esc_html__( 'Total modified products: %d', 'woopam' ),
(int) $total_modified
);
echo '<br/>';
echo '<br/>';
die( esc_html__( 'END', 'woopam' ) );
} );
@hornmedia
Copy link

hornmedia commented Nov 20, 2017

How can i start this plugin ?? :)

@digitalka
Copy link

digitalka commented Jul 25, 2018

Dude I love you. You saved me HUGE amount of work

@lommaker
Copy link

lommaker commented Mar 7, 2019

How can i start this plugin??

@valiermedia
Copy link

valiermedia commented Apr 18, 2019

For those of you who are asking how to start this plugin:

  1. Place the PHP file in a folder named woo-product-attributes-bulk-modifier. It needs to be in the directory root, not in a subfolder.
  2. Either FTP that folder to your plugins directory, or zip the file and go to "Add Plugin" and then upload the zip file via WP
  3. Activate the plugin
  4. Ensure that the product attribute has already been created, this plugin will migrate custom attributes to an EXISTING product attribute. If it hasn't been created, create it.
  5. Visit the following URL (swapping out the variables as needed - variables are in all caps): http://WWW.YOURSITE.COM/?wpq_mode=run&wpq_from=COLOUR&wpq_to=pa_COLOUR&wpq_ppp=100&wpq_offset=0&wpq_post_type=product&wpq_post_status=any

The plugin should list all of the IDs of the products that have been updated. If it only says "Done!" that means that the plugin did not find a matching term for the "to" variable.

@jonfitzgerald
Copy link

jonfitzgerald commented May 1, 2019

Thanks so much for this!

I’m having some trouble though, keep receiving the “Done!” message without any products being updated. Both variables are set correctly.

Is there anything else I should check?

@Majed-RO
Copy link

Majed-RO commented May 28, 2019

A small modification:

instead of ( line 91 ):
// Remove the product meta attribute:
unset( $meta[$from] );

replace it with:
// Remove the product meta attribute:
unset( $meta[$key] );

Because in some cases if you write the custom attribute in a capital letter, it will not unset it.

@wickywills
Copy link

wickywills commented Jul 10, 2019

Doesn't seem to work correctly. For example, I have an attribute named "L/XL", but running this script creates a new attribute called "lxl" and doesn't use the one I created.

@thomastruett
Copy link

thomastruett commented Nov 18, 2019

Are you aware if it is possible to execute the script above and also maintain the variation relationships that were created using the custom attributes? For example, I am migrating from a custom attribute (size) to a taxonomy attribute (pa_size). Before the migration, one of my products has 5 variations based on the custom attribute size (e.g. 10, 12, 14, 16, 18). After I run the migration, it appears that all of the product variations have been orphaned (screenshot attached) and have to be manually re-created.
Screen Shot 2019-11-17 at 18 40 38

@chillpilllike
Copy link

chillpilllike commented Aug 14, 2020

the code is not working with latest woocommerce

@chillpilllike
Copy link

chillpilllike commented Aug 14, 2020

gives the error There was an error somewhere and the terms couldn't be set for post_id

@jlapitan
Copy link

jlapitan commented Aug 17, 2020

Error message says - ** Invalid taxonomy.**

@ecub
Copy link

ecub commented Dec 12, 2021

Plugin doesn't transfer attributes written in languages ​other than English, is it possible to fix it somehow?

@birgire
Copy link
Author

birgire commented Dec 13, 2021

Hi all, thanks for your comments, I forgot about the script for a long time :-)

I will look into the reported problems above.

@birgire
Copy link
Author

birgire commented Dec 13, 2021

I've updated to version 1.0.0 and hopefully fixing the issues mentioned here above.

Notice the new GET parameters names

@birgire
Copy link
Author

birgire commented Dec 13, 2021

Here are some screenshots from the runs:

  1. Nothing do to:

image

  1. Missing product taxonomy:

image

  1. Success:

image

@klemensh
Copy link

klemensh commented Dec 20, 2021

Hello @birgire thanks a lot for this snipped. I'm also trying to convert a custom product attribute in a global attribute (for translation). Unfortunately I get the error message "invalid_taxonomy" even though my global taxonomy does exist. I'm using the latest WooCommerce version.

@birgire
Copy link
Author

birgire commented Dec 21, 2021

Hello @klemensh

I wonder what query string you use and what's the name/slug of the attribute taxonomy?

@waltone57
Copy link

waltone57 commented Jan 31, 2022

This plugin almost seem like answer to my problem but I have same problem than thomastruett earlier. For some reason it just adds global variables to product but does not remove custom one or replace attributes in the stock page. It shows custom attribute with value and global attribute with "any size".

Am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment