Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Last active March 4, 2024 14:51
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save helgatheviking/114c8df50cabb7119b3c895b1d854533 to your computer and use it in GitHub Desktop.
Save helgatheviking/114c8df50cabb7119b3c895b1d854533 to your computer and use it in GitHub Desktop.
Add a custom taxonomy to WooCommerce import/export
<?php
/*
* Plugin Name: WooCommerce Add Taxonomy to Export
* Plugin URI: https://gist.github.com/helgatheviking/114c8df50cabb7119b3c895b1d854533/
* Description: Add a custom taxonomy to WooCommerce import/export.
* Version: 1.0.1
* Author: Kathy Darling
* Author URI: https://kathyisawesome.com/
*
* Woo: 18716:fbca839929aaddc78797a5b511c14da9
*
* Text Domain: woocommerce-product-bundles
* Domain Path: /languages/
*
* Requires at least: 5.0
* Tested up to: 5.0
*
* WC requires at least: 3.5
* WC tested up to: 3.5.4
*
* Copyright: © 2017-2019 SomewhereWarm SMPC.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Add CSV columns for exporting extra data.
*
* @param array $columns
* @return array $columns
*/
function kia_add_columns( $columns ) {
$columns[ 'custom_taxonomy' ] = __( 'Your taxonomy', 'your-text-domain' );
return $columns;
}
add_filter( 'woocommerce_product_export_column_names', 'kia_add_columns' );
add_filter( 'woocommerce_product_export_product_default_columns', 'kia_add_columns' );
/**
* MnM contents data column content.
*
* @param mixed $value
* @param WC_Product $product
* @return mixed $value
*/
function kia_export_taxonomy( $value, $product ) {
$terms = get_terms( array( 'object_ids' => $product->get_ID(), 'taxonomy' => 'your-taxonomy-slug' ) );
if ( ! is_wp_error( $terms ) ) {
$data = array();
foreach ( (array) $terms as $term ) {
$data[] = $term->term_id;
}
$value = json_encode( $data );
}
return $value;
}
add_filter( 'woocommerce_product_export_product_column_custom_taxonomy', 'kia_export_taxonomy', 10, 2 );
/**
* Import
*/
/**
* Register the 'Custom Column' column in the importer.
*
* @param array $columns
* @return array $columns
*/
function kia_map_columns( $columns ) {
$columns[ 'custom_taxonomy' ] = __( 'Your taxonomy', 'your-text-domain' );
return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_options', 'kia_map_columns' );
/**
* Add automatic mapping support for custom columns.
*
* @param array $columns
* @return array $columns
*/
function kia_add_columns_to_mapping_screen( $columns ) {
$columns[ __( 'Your taxonomy', 'your-text-domain' ) ] = 'custom_taxonomy';
// Always add English mappings.
$columns[ 'Your taxonomy' ] = 'custom_taxonomy';
return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'kia_add_columns_to_mapping_screen' );
/**
* Decode data items and parse JSON IDs.
*
* @param array $parsed_data
* @param WC_Product_CSV_Importer $importer
* @return array
*/
function kia_parse_taxonomy_json( $parsed_data, $importer ) {
if ( ! empty( $parsed_data[ 'custom_taxonomy' ] ) ) {
$data = json_decode( $parsed_data[ 'custom_taxonomy' ], true );
unset( $parsed_data[ 'custom_taxonomy' ] );
if ( is_array( $data ) ) {
$parsed_data[ 'custom_taxonomy' ] = array();
foreach ( $data as $term_id ) {
$parsed_data[ 'custom_taxonomy' ][] = $term_id;
}
}
}
return $parsed_data;
}
add_filter( 'woocommerce_product_importer_parsed_data', 'kia_parse_taxonomy_json', 10, 2 );
/**
* Set taxonomy.
*
* @param array $parsed_data
* @return array
*/
function kia_set_taxonomy( $product, $data ) {
if ( is_a( $product, 'WC_Product' ) ) {
if( ! empty( $data[ 'custom_taxonomy' ] ) ) {
wp_set_object_terms( $product->get_id(), (array) $data[ 'custom_taxonomy' ], 'your-taxonomy-slug' );
}
}
return $product;
}
add_filter( 'woocommerce_product_import_inserted_product_object', 'kia_set_taxonomy', 10, 2 );
@efoula
Copy link

efoula commented Nov 4, 2021

Here you go, just change the taxonomy name "suppliers" to your existing custom taxonomy name, and don't forget to change the column name as well in the CSV file, once done, you only need to add the taxonomy term name you want to assign the products to into the column cells.

Cheers!

function add_column_to_importer( $options ) {

  // column slug => column name
  $options['suppliers'] = 'Suppliers';

  return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );


function add_column_to_mapping_screen( $columns ) {
  
  // potential column name => column slug
  $columns['Suppliers'] = 'suppliers';

  return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_column_to_mapping_screen' );


function process_import($object, $data) {
    if (is_a($object, 'WC_Product')) {
        if (!empty($data['suppliers'])) {
            // Save object to assign correct product id
            $object->save();
            wp_set_object_terms( $object->get_id(),  $data['suppliers'], 'suppliers' );
        }
    }
    return $object;
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );

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