I have created two custom taxonomies using the CPT UI plugin named Designers (rug_designers) & Product Lines (product_line). I am working on importing products via a CSV into WooCommerce using the built-in WooCommerce importer tool (not the Product CSV Import Suite). I was able to follow this guide to register the Custom Columns in the import with automatic mapping, however, whenever I upload the CSV file the data does not save to the custom taxonomy.
Here is the current code I have in my functions.php file
* 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['rug_designers'] = 'Designers';
$options['product_line'] = 'Product Line';
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['Designers'] = 'rug_designers';
$columns['designers'] = 'rug_designers';
$columns['Product Lines'] = 'product_line';
return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_column_to_mapping_screen' );
/**
* Process the data read from the CSV file.
* This just saves the value in meta data, but you can do anything you want here with the data.
*
* @param WC_Product $object - Product being imported or updated.
* @param array $data - CSV data read for the product.
* @return WC_Product $object
*/
function process_import( $object, $data ) {
// This appears to be part I am having trouble with, I have even tried it with just the 'rug_designers'
if ( ! empty( $data['rug_designers'] ) || ! empty( $data['product_line'] ) ) {
$object->update_meta_data( 'rug_designers', $data['rug_designers'] );
$object->update_meta_data( 'product_line', $data['product_line'] );
}
return $object;
}
As my comment above mentions, I believe the issue to be in the process import function. I have tried to run this without the ‘product_line’ code and was still unable to get the ‘rug_designers’ to update the data upon csv import.
I would highly appreciate any suggestions. Thanks.
2
Answers
I was able to accomplish the importing of custom taxonomies/post types by purchasing the Woocommerce Product CSV Import Suite. It worked right out of the box and saved me tons of time from trying to write the code from scratch.
Looking for a solution to do this myself. I’ve used the CPT UI plugin to create Vendors. Didn’t have a problem following the docs to add the columns, but it’s mapping the columns to the custom taxonomies that’s the problem. Hope this comment will bump the question and get us an answer. Cheers!