• Resolved Apfelbiss

    (@apfelbiss)


    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support kellymetal a11n

    (@kellymetal)

    Hi there,

    I changed the second half of your first snippet (the line where it grabs the slug of the product), and it seemed to work when I added to my child theme’s functions.php. My product export included a “Slug” column with the slugs of all my products.

    
    /**
     * 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 ) {
        $slug = $product->get_slug();
        return $slug;
    }
    // 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 );
    

    As for importing, that other forum post had a link to an article that seems to cover that:
    https://blog.oxrud.com/posts/import-slug-into-woocommerce/

    I hope that helps!

    Plugin Support EtienneP a11n

    (@etiennep)

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding Custom Product Export Columns?’ is closed to new replies.