Import Custom Taxonomy to Woocommerce by CSV File

Suppose you want to load a large amount of items into the Woocommerce plugin that have other metadata besides tags or categories.

Of course you can do a bulk upload via csv files with Woocommerce. But further information can only be imported as „attributes“, but not as taxonomies by default.

An alternative seems to be paid plugins. I show a way to import custom taxonomies with the existing WooCommerce Importer without a (third-party) plugin.

1) Create a Custom Taxonomy

A taxonomy is meta data of a product, just like the tags of a product. Basically there are two ways to create them: Either they are registered via a snippet using the register_taxonomy() function or created via a plugin (ex. Custom Post Type UI).

function register_custom_taxonomy() {
    register_taxonomy(
        'custom_taxonomy',
        'product', // we register the taxonomy for prodcuts
        array(
            'label' => __( 'Custom Taxonomy' )
        )
    );
}
add_action( 'init', 'register_custom_taxonomy' );

Read more: Difference between custom taxonomy and custom field types

2) Extend the import function of Woocommerce

This is the more difficult part. The built-in importer of WooCommerce can do quite a lot, as the documentation shows. For example, WooCommerce provides a detailed scheme of how a csv-file has to look like to upload also complex data.

Metadata or product properties can basically be imported in three ways:

  1. as attributes
  2. as custom fields
  3. as custom taxonomy

Option 1 is can be used out-of-the-box by default, if the columns in the csv correspond to the official Product CSV Import schema.

Option 2 can also be achieved by following the documentation and then extending the template. See: Display a custom field in a Woocommerce WordPress Template

The way to Option 3 is kind of obscure. My suspicion is that Woocommerce does not aggressively promote or document this feature because it offers a paid plugin that serves exactly this use case. I do not condemn this, it is a reasonable business decision.

Basically, there are two things to be done: (1) inform the WooCommerce Importer that there will be a new, unknown column and (2) upload the values from this column as terms of a custom taxonomy.

The following code is based on WooCommerce’s code to add support of custom columns in a csv file during mapping and a very helpful gist with good comments for the actual import.

The sample data:

ID,Name,custom_taxonomy,Categories
0,"product name","custom term 1, custom term 2","Test"

2.1) Register Adding Custom Column

After adding support for custom columns the CSV Importer adds it to the mapping options.

/**
* Source: https://github.com/woocommerce/woocommerce/wiki/Product-CSV-Importer-&-Exporter#adding-custom-import-columns-developers
**/
/**
 * Register the 'Custom Column' column in the importer.
 *
 * @param array $options
 * @return array $options
 */
function add_column_to_importer( $options ) {
	// column slug => column name
	$options['custom_taxonomy'] = 'Custom Taxonomy';
	return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );
/**
 * Add automatic mapping support for 'Custom Column'. 
 * This will automatically select the correct mapping for columns named 'Custom Column' or 'custom column'.
 *
 * @param array $columns
 * @return array $columns
 */
function add_column_to_mapping_screen( $columns ) {
	
	// potential column name => column slug
	$columns['Custom Taxonomy'] = 'custom_taxonomy';
	$columns['custom taxonomy'] = 'custom_taxonomy';
	return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_column_to_mapping_screen' );

2.2) Importing Taxonomy Terms

Next, we add a function that imports the values of the custom taxonomy column as terms of a custom taxonomy (source).

/**
 * Set taxonomy.
 *
 * @param  array  $parsed_data
 * @return array
 */
function woocommerce_add_custom_taxonomy( $product, $data ) {
  
  // set a variable with your custom taxonomy slug
  $custom_taxonomy = 'custom_taxonomy';
	if ( is_a( $product, 'WC_Product' ) ) {
		if( ! empty( $data[ $custom_taxonomy ] ) ) {
            		$product->save();
            		$custom_taxonomy_values = $data[ $custom_taxonomy ];
            		$custom_taxonomy_values = explode(",", $custom_taxonomy_values);
            		$terms = array();
            		foreach($custom_taxonomy_values as $custom_taxonomy_value){
                		if(!get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)){
                    			$custom_taxonomy_args= array(
                        			'cat_name' => $custom_taxonomy_value,
                        			'taxonomy' => $custom_taxonomy,
                    			);
                    			$custom_taxonomy_value_cat = wp_insert_category($custom_taxonomy_args);
                    			array_push($terms, $custom_taxonomy_value_cat);
                		}else{
                    			$custom_taxonomy_value_cat = get_term_by('name', $custom_taxonomy_value, $custom_taxonomy)->term_id;
                    			array_push($terms, $custom_taxonomy_value_cat);
                		}
            		}
			wp_set_object_terms( $product->get_id(),  $terms, $custom_taxonomy );
		}
	}
	return $product;
}

3) Display Woocommerce Custom Taxonomy

You have to options to display your custom terms of a taxonomy: via a hook or via a template file. I chose to include it in the template file theme_name/woocommerce/single-product/meta.php:

<?php the_terms( $post->ID, 'custom_taxonomy', 'Displayed name of custom taxonomy: ', ', ', ' ' ); ?>

(If you want to display a custom field, use this snippet):

<?php $custom_field = get_post_meta( $product->get_id(), 'custom_field_name', true );
    if ( is_string( $custom_field ) ) { 
        echo wp_kses_post( "<p>custom field: $custom_field</p>" );
    };
d?

4 Kommentare

Michael 24. Februar 2021 Antworten

Thanks so much for this great tutorial! Please note that in step 2.2 the code is missing the add_filter() function. Just append this line to the code:

add_filter( ‚woocommerce_product_import_inserted_product_object‘, ‚woocommerce_add_custom_taxonomy‘, 10, 2 );

Rocky 28. September 2021 Antworten

IN my case, In woocommerce_add_custom_taxonomy function, When I print $data, I see that $data does not contain custom_taxonomy information. It is empty. Custom Taxonomy still not getting imported.

Jass 30. November 2021 Antworten

This worked for me, thank you! I have 3 custom taxonomies related to my products and they’re all importing now.

Abgr Xenda 19. Juli 2022 Antworten

it worked for me smoothly – just need to add add-filter as follow

add_filter(‚woocommerce_product_import_inserted_product_object‘,’woocommerce_add_custom_taxonomy‘, 10, 2 );

Schreibe einen Kommentar