沃梦达 / 编程问答 / php问题 / 正文

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

Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中将自定义列添加到管理产品列表)

本文介绍了在 WooCommerce 3 中将自定义列添加到管理产品列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Woocommerce 管理产品列表中添加交货时间"列?

Is it possible to add a column "delivery time" to the Woocommerce admin product list?

我知道在屏幕选项"中有一些额外的列(拇指、价格、product_cat 等)可供选择,但交货时间"不可用.

I know there are some additional columns (thumb, price, product_cat etc) to choose at "Screen Options" but "delivery time" is not available.

是否可以以某种方式将其添加到列表中?

Is it possible somehow to add it to the list?

我尝试按照 LoicTheAztecs 的回答进行操作,但在找到正确的 meta_key slug 时遇到问题.

I tried to follow LoicTheAztecs answer, but I'm having problems to find the correct meta_key slug.

如果我在 wp_postmeta 中搜索delivery",我会得到 0 个结果.

If i search for "delivery" in wp_postmeta I'm getting 0 results.

但是有些产品指定了交货时间.在我的产品页面上有一个文本字段Lieferzeit: 1–2 Wochen"(表示交货时间:1–2 周).如果我在整个数据库中搜索Wochen",我会在 wp_options 中获得 2 次点击,在 wp_terms 中获得 6 次点击.

But there are products with delivery time assigned. On my product page there's a text field "Lieferzeit: 1–2 Wochen" (means Delivery time: 1–2 weeks). If I search the whole database for "Wochen" I'm getting 2 hits in wp_options und 6 hits in wp_terms.

数据库一般搜索:

wp_terms 数据库中的点击次数:

Hits in wp_terms DB:

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

Do you know how to find the correct meta_key slug from here?

推荐答案

这里是将 2 个自定义函数挂钩的方法.第一个用标题创建列,第二个用产品数据填充列.但是您需要在第二个函数中设置正确对应的meta_key 以获取数据.

Here is the way to do it with that 2 custom functions hooked. The first one create the column with the title, the second one populate the column with the products data. But you will need to set in that second function, the correct corresponding meta_key to get the data.

代码如下:

// 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 在您的数据库中进行搜索.您必须以这种方式在 wp_postmeta 表中搜索 delivery 术语:

To find the correct meta_key slug corresponding to the "delivery time", you should need to make search in your database using PhpMyAdmin. You will have to search for delivery term in wp_postmeta table this way:

然后你会得到这样的结果(这里只有 1 行带有假 slug):

Then you will get this kind of results (here there is just 1 line with a fake slug):

所以现在你应该能够得到正确的 slug 名称​​(比如这个假的_delivery_date") ...

So now you should be able to get the correct slug name (like this fake "_delivery_date" one)

相关答案(针对订单):将自定义列添加到 WooCommerce 后端的管理订单列表

Related answer (for orders): Add custom columns to admin orders list in WooCommerce backend

这篇关于在 WooCommerce 3 中将自定义列添加到管理产品列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 WooCommerce 3 中将自定义列添加到管理产品列表

基础教程推荐