• Hi there,

    we are facing one issue related custom taxonomy import/export CSV functionality.on our site there is two custom taxonomy called category brand and manufacturer when we export CSV of product that time it did not show any field regarding it on the other hand when we import it we aren’t able to Map CSV fields to products.we also have tried this code https://github.com/woocommerce/woocommerce/wiki/Product-CSV-Importer-&-Exporter#adding-custom-import-columns-developers but not getting any result so is that possible or not and is that bug or something else.could provide any reference code or we have to buy plugins like Product CSV Import Suite, WP All Import plugin ? we preferring code rather than the plugin.

    please help us out.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter marsmp

    (@marsmp)

    Hi there,

    we have mange to import custom taxonomy by using bellow code.
    note:But we only able import single taxonomy not nested taxonomy is that any way anyone can helps out?

    we have found Reference code and modify it are below:-

    https://gist.github.com/helgatheviking/114c8df50cabb7119b3c895b1d854533

    /* Add Custom Taxonomy(Manufacture) in Product Function Start */

    function custom_taxonomy() {
    
    	$labels = array(
    		'name'                       => _x( 'Manufacture', 'Taxonomy General Name', 'text_domain' ),
    		'singular_name'              => _x( 'Manufacture', 'Taxonomy Singular Name', 'text_domain' ),
    		'menu_name'                  => __( 'Manufacture', 'text_domain' ),
    		'all_items'                  => __( 'All Items', 'text_domain' ),
    		'parent_item'                => __( 'Parent Item', 'text_domain' ),
    		'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
    		'new_item_name'              => __( 'New Item Name', 'text_domain' ),
    		'add_new_item'               => __( 'Add New Item', 'text_domain' ),
    		'edit_item'                  => __( 'Edit Item', 'text_domain' ),
    		'update_item'                => __( 'Update Item', 'text_domain' ),
    		'view_item'                  => __( 'View Item', 'text_domain' ),
    		'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
    		'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
    		'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    		'popular_items'              => __( 'Popular Items', 'text_domain' ),
    		'search_items'               => __( 'Search Items', 'text_domain' ),
    		'not_found'                  => __( 'Not Found', 'text_domain' ),
    		'no_terms'                   => __( 'No items', 'text_domain' ),
    		'items_list'                 => __( 'Items list', 'text_domain' ),
    		'items_list_navigation'      => __( 'Items list navigation', 'text_domain' ),
    	);
    	$args = array(
    		'labels'                     => $labels,
    		'hierarchical'               => true,
    		'public'                     => true,
    		'show_ui'                    => true,
    		'show_admin_column'          => true,
    		'show_in_nav_menus'          => true,
    		'show_tagcloud'              => true,
    	);
    	register_taxonomy( 'manufacture', array( 'product' ), $args );
    
    }
    add_action( 'init', 'custom_taxonomy', 0 );

    /* Add Custom Taxonomy(Manufacture) in Product Function End*/

    /*********logic code for custom taxonomy************/

    /**
     * Import shop section of the products from the CSV file
     */
    
    /**
     * Register the 'Custom Column' column in the importer.
     *
     * @param  array  $options
     * @return array  $options
     */
    function kia_map_columns( $options ) {
    	$options[ 'manufacture' ] = __( 'Manufacture', 'woocommerce' );
    	return $options;
    }
    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[ __( 'Manufacture', 'woocommerce' ) ] 	= 'manufacture';
    
    	// Always add English mappings.
    	$columns[ 'Manufacture' ]	= 'manufacture';
    
    	return $columns;
    }
    add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'kia_add_columns_to_mapping_screen' );
    
    /**
     * Set taxonomy.
     *
     * @param  array  $parsed_data
     * @return array
     */
    function kia_set_taxonomy( $product, $data ) {
    
    	if ( is_a( $product, 'WC_Product' ) ) {
    
    		if( ! empty( $data [ 'manufacture' ] ) ) {
    			wp_set_object_terms( $product->get_id(),  (array) $data ['manufacture'], 'manufacture' );
    		}
    
    	}
    
    	return $product;
    }
    add_filter( 'woocommerce_product_import_inserted_product_object', 'kia_set_taxonomy', 10, 2 );

    /*********for custom brand taxonomy***********/

    
    function brand_map_columns( $options ) {
    	$options[ 'product_brand' ] = __( 'Product Brand', 'woocommerce' );
    	return $options;
    }
    add_filter( 'woocommerce_csv_product_import_mapping_options', 'brand_map_columns' );
    
    function kia_add_brand_columns_to_mapping_screen( $columns ) {
    
    	$columns[ __( 'Product Brand', 'woocommerce' ) ] 	= 'product_brand';
    
    	// Always add English mappings.
    	$columns[ 'Product Brand' ]	= 'product_brand';
    
    	return $columns;
    }
    add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'kia_add_brand_columns_to_mapping_screen' );
    
    function brand_set_taxonomy( $product, $data ) {
    
    	if ( is_a( $product, 'WC_Product' ) ) {
    
    		if( ! empty( $data [ 'product_brand' ] ) ) {
    			wp_set_object_terms( $product->get_id(),  (array) $data ['product_brand'], 'product_brand' );
    		}
    
    	}
    
    	return $product;
    }
    add_filter( 'woocommerce_product_import_inserted_product_object', 'brand_set_taxonomy', 10, 2 );
    • This reply was modified 5 years, 11 months ago by marsmp.

    I had it working but it was only moving the item_ID Ive been messing with it to use the item_name field instead but to no luke and I wanted to share what i gut before aI pass out

    <?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: https://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' ] = __( 'Vape Types', 'vape-type-taxonomy'  );
    	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' => 'vape_types'    ) );
    
    	if ( ! is_wp_error( $terms ) ) {
    
    		$data = array();
    
    		foreach ( (array) $terms as $term ) {
    			$data[] = $term_name;
    		}
    
    		$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[ 'vape_types' ] = __( 'Vape Types', 'vape-type-taxonomy' );
    	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[ __( 'Vape Types', 'vape-type-taxonomy' ) ] 	= 'vape_types';
    
    	// Always add English mappings.
    	$columns[ 'Vape Types' ]	= 'Vape Types';
    
    	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[ 'Vape Types' ] ) ) {
    
    		$data = json_decode( $parsed_data[ 'Vape Types' ], true );
    
    		unset( $parsed_data[ 'Vape Types' ] );
    
    		if ( is_array( $data ) ) {
    
    			$parsed_data[ 'Vape Types' ] = array();
    
    			foreach ( $data as $term ) {
    				$parsed_data[ 'Vape Types' ][] = $term_name;
    			}
    		}
    	}
    
    	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[ 'Vape Types' ] ) ) {
    			wp_set_object_terms( $product->get_id(),  (array) $data[ 'Vape Types' ], 'vape_types' );
    		}
    
    	}
    
    	return $product;
    }
    add_filter( 'woocommerce_product_import_inserted_product_object', 'kia_set_taxonomy', 10, 2 );
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘we are facing one issue related custom taxonomy import/export CSV functionality’ is closed to new replies.