将一个具有作者姓名的新列添加到WooCommerce管理员优惠券列表中 [英] Add a new column with author name to WooCommerce admin coupon list

查看:49
本文介绍了将一个具有作者姓名的新列添加到WooCommerce管理员优惠券列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我们有这样的情况,有人问我谁创建了这张优惠券?".不幸的是,默认情况下,WooCommerce不会在列出所有优惠券的优惠券概述中显示优惠券的创建者.

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.

我试图找出的是,如何在WooCommerce>中添加一个具有作者姓名的新列.行销>优惠券概述.

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?

推荐答案

这将在优惠券列表中添加一个新列,其中包含作者的名字,通过添加到代码中的注释标签进行解释.

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

这篇关于将一个具有作者姓名的新列添加到WooCommerce管理员优惠券列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆