Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save SashkoWooTeam/3acb254d044be23d35af3242c9bfb2d5 to your computer and use it in GitHub Desktop.

Select an option

Save SashkoWooTeam/3acb254d044be23d35af3242c9bfb2d5 to your computer and use it in GitHub Desktop.
Adding Custom Import/Export Columns (WooCommerce CSV)
/**
* Add the custom column to the exporter and the exporter column menu.
*
* @param array $columns
* @return array $columns
*/
function jet_engine_add_wc_export_column( $columns ) {
// column slug => column name
$columns['product_custom_text'] = 'Product Custom Text';
return $columns;
}
add_filter( 'woocommerce_product_export_column_names', 'jet_engine_add_wc_export_column' );
add_filter( 'woocommerce_product_export_product_default_columns', 'jet_engine_add_wc_export_column' );
/**
* Provide the data to be exported for one item in the column.
*
* @param mixed $value (default: '')
* @param WC_Product $product
* @return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function jet_engine_add_wc_export_data( $value, $product ) {
$value = $product->get_meta( 'product_custom_text', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_product_custom_text', 'jet_engine_add_wc_export_data', 10, 2 );
/**
* Register the 'Custom Column' column in the importer.
*
* @param array $options
* @return array $options
*/
function jet_engine_add_wc_column_to_importer( $options ) {
// column slug => column name
$options['product_custom_text'] = 'Product Custom Text';
return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_options', 'jet_engine_add_wc_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 jet_engine_add_wc_column_to_mapping_screen( $columns ) {
// potential column name => column slug
$columns['Product Custom Text'] = 'product_custom_text';
$columns['product custom text'] = 'product_custom_text';
return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'jet_engine_add_wc_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 jet_engine_wc_process_import( $object, $data ) {
if ( ! empty( $data['product_custom_text'] ) ) {
$object->update_meta_data( 'product_custom_text', $data['product_custom_text'] );
}
return $object;
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'jet_engine_wc_process_import', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment