Adding Custom Product Export Columns?
-
Hello,
is it possible to add the Slug within product export via Snippet?
I tried to add the product slug within the Product Export as described at Adding Custom Export Columns (Developers).
I tried it on several ways, but it’s a little bit tricky, because the described way only recognizes information from “postmeta” table and not “posts” table in the database.
My Snippet (version 1):
/** * Add the custom column to the exporter and the exporter column menu. * * @param array $columns * @return array $columns */ function add_export_column( $columns ) { // column slug => column name $columns['post_name'] = 'Slug'; return $columns; } add_filter( 'woocommerce_product_export_column_names', 'add_export_column' ); add_filter( 'woocommerce_product_export_product_default_columns', 'add_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 add_export_data( $value, $product ) { $value = $product->get_meta( 'post_name', 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_post_name', 'add_export_data', 10, 2 );
My Snippet (version 2, with changed “function add_export_data”):
/** * Add the custom column to the exporter and the exporter column menu. * * @param array $columns * @return array $columns */ function add_export_column( $columns ) { // column slug => column name $columns['post_name'] = 'Slug'; return $columns; } add_filter( 'woocommerce_product_export_column_names', 'add_export_column' ); add_filter( 'woocommerce_product_export_product_default_columns', 'add_export_column' ); /** * Provide the data to be exported for one item in the column.a * * @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 add_export_data( $value, $product ) { global $wp_query; $postid = $wp_query->post->ID; $value = get_post_field( 'post_name', get_post($postid) ); return $value; } // Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'. add_filter( 'woocommerce_product_export_product_column_post_name', 'add_export_data', 10, 2 );
I know the discussion thread Product Export/Import doesn’t include slug and also the on that page linked GitHub thread [CSV Import] Add slug to CSV import and/or truncate attribute names to form a slug < 26 chars #23153.
Beside the fact that I don’t see the connection between the Slug export and the attribute discussion at GitHub, it would be nice to have a way by myself to export the Slug.
Thank you in advance.
- The topic ‘Adding Custom Product Export Columns?’ is closed to new replies.