Skip to content
Advertisement

Add a new column with author name to WooCommerce admin coupon list

Yesterday we had the situation, that someone ask my “Who created this coupon?”. Unfortunately WooCommerce by default does not display the creator of the coupon in the coupon overview where all coupon are listed.

What I try to find out is, how can I add a new column with the author name in the WooCommerce > Marketing > Coupons overview.

This what I have so far:

function display_coupon_creator() {

    foreach( $coupons as $coupon ){

        // Get coupon creator
        $coupon_creator_id = get_post_field('post_author', $post_id);
        $creator_name = get_the_author_meta( 'display_name', $coupon_creator_id );

        echo $creator_name . '<br>';    
    }
}

Unfortunately without the desired result, is there someone who can put me on the right track?

Advertisement

Answer

This will add a new column to the coupon list with the author’s name, explanation via comment tags added to the code.

// Add a Header
function filter_manage_edit_shop_coupon_columns( $columns ) {   
    // Add new column
    $columns['coupon_author'] = __( 'Author', 'woocommerce' );

    return $columns;
}
add_filter( 'manage_edit-shop_coupon_columns', 'filter_manage_edit_shop_coupon_columns', 10, 1 );

// Populate the Column
function action_manage_shop_coupon_posts_custom_column( $column, $post_id ) {
    // Compare
    if ( $column == 'coupon_author' ) {
        // Author ID
        $author_id = get_post_field ( 'post_author', $post_id );
        
        // Display name
        $display_name = get_the_author_meta( 'display_name' , $author_id );
        
        // NOT empty
        if ( ! empty ( $display_name ) ) {
            echo $display_name;         
        }
    }
}
add_action( 'manage_shop_coupon_posts_custom_column' , 'action_manage_shop_coupon_posts_custom_column', 10, 2 );
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement