Skip to content
PHP

WooCommerce Bulk Product Attribute Modifier – Convert Attributes to Taxonomies

A
Author
Mar 20, 2025 · 5 min read
WooCommerce Bulk Product Attribute Modifier – Convert Attributes to Taxonomies

WooCommerce is a powerful eCommerce platform, but sometimes its default settings don’t provide the level of customization required by store owners. One common challenge is modifying product attributes in bulk, especially when converting them into taxonomies. In this article, we will walk through a custom PHP solution that enables administrators to efficiently modify WooCommerce product attributes in bulk.


Understanding Product Attributes in WooCommerce

Product attributes in WooCommerce allow store owners to define product specifications such as size, color, and material. These attributes are typically managed as metadata within each product entry. However, for better organization and filtering, it is often beneficial to convert them into taxonomies.

A taxonomy in WordPress is a way to group posts together. WooCommerce uses taxonomies for categories, tags, and product attributes. The advantage of using taxonomies over metadata is that they are indexed in the database, making searches and filtering significantly faster.


Why Modify Product Attributes in Bulk?

WooCommerce store owners might need to modify product attributes for several reasons:

  • Migrating from one attribute structure to another.
  • Enhancing filtering and sorting capabilities on the store.
  • Improving SEO and product discoverability.
  • Streamlining product management for a large inventory.

If your store has hundreds or even thousands of products, manually modifying attributes can be time-consuming. That’s where automation comes in.


The PHP Solution: Bulk Modifying Product Attributes

The PHP script we provide below automates the process of modifying product attributes in WooCommerce. It retrieves all products with a specified attribute, replaces that attribute with a new one, and updates the product data accordingly.

Code Breakdown

Let’s break down the key elements of the code:

  1. Hooking into WordPress
    The script uses the template_redirect action hook to trigger the function when a specific URL parameter (wpq_mode=run) is detected. This ensures that the script is only executed when explicitly requested.
  2. Checking Admin Privileges
    Only administrators (manage_options capability) can run this script, preventing unauthorized users from making modifications.
  3. Fetching User Input
    The script accepts several URL parameters:
    • wpq_from: The current attribute name to be replaced.
    • wpq_to: The new attribute name.
    • wpq_post_type: The type of posts to modify (default is ‘product’).
    • wpq_post_status: The status of the products (default is ‘any’).
    • wpq_ppp: Number of products to process per batch (default is 10).
    • wpq_offset: Offset for pagination (default is 0).
  4. Querying Products with the Given Attribute
    The script retrieves all products that have the specified attribute using get_posts(), filtering by post type, status, and metadata.
  5. Modifying Attributes
    If a product contains the specified attribute (wpq_from), the script:
    • Removes the old attribute.
    • Creates a new attribute (wpq_to) as a taxonomy.
    • Assigns the new taxonomy terms to the product.
  6. Updating Product Data
    Finally, the modified attributes are saved back to the database using update_post_meta(), ensuring WooCommerce recognizes the changes.

Full PHP Code: Bulk Modifier for WooCommerce Product Attributes

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( 'The "from" and "to" parameters 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 )
        {
            if( $from === $terms['name'] )
            {
                $product_needs_update  = true;
                $tmp                   = explode( '|', $terms['value'] );         
                $product_terms         = array_map( 'sanitize_key', $tmp );

                // Remove old attribute:
                unset( $meta[$from] );        

                // Add new attribute as taxonomy:
                $meta["{$to}"] = array( 
                    'name'        => "{$to}",
                    'value'       => '',
                    'position'    => $terms['position'], 
                    'is_visible'  => $terms['is_visible'], 
                    'is_variation'=> $terms['is_variation'], 
                    'is_taxonomy' => 1, 
                );  
            }
        }

        if( $product_needs_update )
        {
            $term_taxonomy_ids = wp_set_object_terms( $post_id, $product_terms, $to, false );
            if ( is_wp_error( $term_taxonomy_ids ) )
            {
                printf( "Error: Could not set terms for post_id: %d <hr/>", $post_id );
            } 
            else
            {
                update_post_meta( $post_id, $meta_key, $meta );
                printf( "Success! Attributes updated for post_id: %d <hr/>", $post_id );
            }
        }
    }
    die( 'Done!');
}});

How to Execute This Script

  1. Add the Code to Your Theme
    Place the above code in your theme’s functions.php file or in a custom plugin.
  2. Run the Script via URL
    Navigate to your WordPress site and use the following URL format:
https://thecreativecoders.com/?wpq_mode=run&wpq_from=old_attribute&wpq_to=new_attribute

Replace old_attribute with the existing attribute name and new_attribute with the new taxonomy name.

Monitor Output
The script will print success or error messages directly on the screen.

A
Author

Author at The Creative Coders.

The monthly build log

One email a month. New posts, lessons, and the occasional strong opinion. No spam.