問題描述
是否可以在 Woocommerce 管理產(chǎn)品列表中添加交貨時間"列?
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 個結(jié)果.
If i search for "delivery" in wp_postmeta I'm getting 0 results.
但是有些產(chǎn)品指定了交貨時間.在我的產(chǎn)品頁面上有一個文本字段Lieferzeit: 1–2 Wochen"(表示交貨時間:1–2 周).如果我在整個數(shù)據(jù)庫中搜索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.
數(shù)據(jù)庫一般搜索:
wp_terms
數(shù)據(jù)庫中的點擊次數(shù):
Hits in wp_terms
DB:
你知道如何從這里找到正確的 meta_key slug 嗎?
Do you know how to find the correct meta_key slug from here?
推薦答案
這里是將 2 個自定義函數(shù)掛鉤的方法.第一個用標(biāo)題創(chuàng)建列,第二個用產(chǎn)品數(shù)據(jù)填充列.但是您需要在第二個函數(shù)中設(shè)置正確對應(yīng)的meta_key
以獲取數(shù)據(jù).
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 文件或任何插件文件中.
經(jīng)過測試并有效.
如何獲得正確的 meta_key slug:
要找到與交付時間"對應(yīng)的正確meta_key
slug,您需要使用PhpMyAdmin 在您的數(shù)據(jù)庫中進行搜索.您必須以這種方式在 wp_postmeta
表中搜索 delivery
術(shù)語:
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:
然后你會得到這樣的結(jié)果(這里只有 1 行帶有假 slug):
Then you will get this kind of results (here there is just 1 line with a fake slug):
所以現(xiàn)在你應(yīng)該能夠得到正確的 slug 名稱??(比如這個假的_delivery_date") ...
So now you should be able to get the correct slug name (like this fake "_delivery_date" one) …
相關(guān)答案(針對訂單):將自定義列添加到 WooCommerce 后端的管理訂單列表
Related answer (for orders): Add custom columns to admin orders list in WooCommerce backend
這篇關(guān)于在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!