在WooCommerce 3中将自定义列添加到管理产品列表

问题描述 投票:2回答:1

是否可以在Woocommerce管理产品列表中添加“交付时间”列?

我知道在“屏幕选项”中有一些额外的列(thumb,price,product_cat等)可供选择,但“交付时间”不可用。

有可能以某种方式将其添加到列表中吗?


编辑:

我试图跟随LoicTheAztecs的回答,但是我遇到了找到正确的meta_key slug的问题。

如果我在wp_postmeta中搜索“delivery”,我得到0结果。

但是有些产品的交货时间已分配。在我的产品页面上有一个文本字段“Lieferzeit:1-2 Wochen”(表示交货时间:1-2周)。如果我在整个数据库中搜索“Wochen”,我在wp_options中获得2次点击,在wp_terms中获得6次点击。

DB一般搜索:

db search


点击wp_terms DB:

hits in wp_terms


你知道如何从这里找到正确的meta_key slug吗?

php wordpress woocommerce custom-fields product
1个回答
4
投票

以下是使用2个自定义函数挂钩的方法。第一个用标题创建列,第二个用产品数据填充列。但是你需要设置第二个函数,正确对应的meta_key来获取数据。

这是代码:

// ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST
add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
function custom_product_column($columns)
{
   //add columns
   $columns['delivery'] = __( 'Delivery time','woocommerce'); // title
   return $columns;
}

// ADDING THE DATA FOR EACH PRODUCTS BY COLUMN (EXAMPLE)
add_action( 'manage_product_posts_custom_column' , 'custom_product_list_column_content', 10, 2 );
function custom_product_list_column_content( $column, $product_id )
{
    global $post;

    // HERE get the data from your custom field (set the correct meta key below)
    $delivery_time = get_post_meta( $product_id, '_delivery_time', true );

    switch ( $column )
    {
        case 'delivery' :
            echo $delivery_time; // display the data
            break;
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

经过测试和工作。


如何获得正确的meta_key slug:

要找到与“交货时间”对应的正确meta_key slug,您需要使用PhpMyAdmin在数据库中进行搜索。您必须以这种方式在delivery表中搜索wp_postmeta术语:

enter image description here

然后你会得到这样的结果(这里只有一条假子弹线):

enter image description here

所以现在你应该能够获得正确的slug名称(就像这个假的“_delivery_date”一样)......


相关答案(订单):Add custom columns to admin orders list in WooCommerce backend

© www.soinside.com 2019 - 2024. All rights reserved.