Pawan Kumawat

How to know “Who created this coupon?”

What to do when your client ask “Who created this coupon?” . in WooCommerce.  But we don’t have any default things in WooCommerce  which automatically show author of coupon .

So we can use simple posts_custom_column hooks for it. Which is actually very useful in adding new column in wordpress tables .

 

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

 

Above code will add new column  to the coupon list with the author’s name.

 

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

 

Above code is for displaying author name after particular coupon row.

 

Share This On :

Leave a Comment

Your email address will not be published. Required fields are marked *

Categories
Scroll to Top

Request A Quote