Forked from helgatheviking/add-taxonomy-to-woocommerce-export.php
Created
July 27, 2020 16:56
-
-
Save tsbega/739cd6b73b4e6fc8cec0068a8bd95158 to your computer and use it in GitHub Desktop.
Add a custom taxonomy to WooCommerce import/export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment