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

痴心易碎 提交于 2021-01-27 20:23:07

问题


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?


回答1:


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 );


来源:https://stackoverflow.com/questions/65653372/add-a-new-column-with-author-name-to-woocommerce-admin-coupon-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!