How to add a field to the WooCommerce product CSV exporter
The WooCommerce Product CSV Importer and Exporter is a handy built-in tool for exporting product details. In my experience, the tool usually has sufficient functionality for my exporting needs, but I occasionally need extra functionality. While I can get a completely customized export with WP All Export, using a plugin like that for small functionality adds is overkill.
Depending on your needs, a few lines of code could extend the functionality of the built-in product CSV exporter just as well and save you purchasing/loading a heavyweight plugin like WP All Export.
Coding Example
For example, say you just wanted to add the “Published Date” as a field to the product CSV exporter. Thanks to WooCommerce’s abundant use of action/filter hooks, we can easily do that using the following filters.
Simply copy/paste the following filters into your child theme’s functions.php file:
// Add our custom columns to the existing columns
add_filter( 'woocommerce_product_export_product_default_columns', 'custom_product_export_columns', 11 );
function custom_product_export_columns( $columns ) {
// The key/id "published_date" is what we will use in the next filter to define the column's value
$columns['published_date'] = __( 'Published Date', 'woocommerce' );
return $columns;
}
// Return the desired value for the "published_date" column
add_filter( 'woocommerce_product_export_product_column_published_date', 'custom_product_export_column_published_date', 11, 3 );
function custom_product_export_column_published_date( $value, $product, $column_id ) {
return $product->get_date_created()->date( 'Y/m/d' );
}
Or use the following if you prefer anonymous functions:
// Add our custom columns to the existing columns
add_filter( 'woocommerce_product_export_product_default_columns', function ( $columns ) {
// The key/id "published_date" is what we will use in the next filter to define the column's value
$columns['published_date'] = __( 'Published Date', 'woocommerce' );
return $columns;
}, 11 );
// Return the desired value for the "published_date" column
add_filter( 'woocommerce_product_export_product_column_published_date', function ( $value, $product, $column_id ) {
return $product->get_date_created()->date( 'Y/m/d' );
}, 11, 3 );
Need more help?
As always, just reach out if you need more help. You can contact me through my request a free estimate form.