在显示产品名称的 WooCommerce 管理订单列表中添加新列时出现问题
Issue when adding a new column in WooCommerce admin order list displaying product names
我正在尝试在“订单”页面(管理员)上显示一列。在该列中,显示订单中的每个产品。
想法是能够快速查看已订购的产品。
添加列工作正常,但无法获取产品。我得到的只是 Fatal error: Uncaught ArgumentCountError
代码如下:
add_filter( 'manage_edit-shop_order_columns', 'products_column_create_products', 20 );
function products_column_create_products( $columns ) {
$reordered_columns = array();
foreach( $columns as $key => $column ) {
$reordered_columns[$key] = $column;
if ( $key == 'order_number' ) {
$reordered_columns['products'] = __( 'Products','woocommerce' );
}
}
return $reordered_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'products_column_show_products', 20, 3 );
function products_column_show_products($column, $post_id, $order){
if ('products' != $column) return;
global $order;
$details = array();
foreach($order->get_items() as $item)
$details[] = '<a href="' . $item->get_product()->get_permalink() . '">' . $item->get_name() . '</a> × ' . $item->get_quantity() .'<br>';
echo count($details) > 0 ? implode('<br>', $details) : '–';
}
我不知道如何解决,所以如果有人可以提供建议,请告诉我
您的代码有一些小错误,例如:
- 您指出
products_column_show_products()
回调函数需要 3 个参数,而 manage_{$post->post_type}_posts_custom_column
WordPress 挂钩包含 2 个参数。 $column_name
和 $post_id
- 不需要使用
global $order;
,通过$post_id
,
您可以使用 wc_get_order( $post_id );
访问订单
所以这应该足够了:
// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
// Loop trough existing columns
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// Add after order status column
if ( $key === 'order_number' ) {
$new_columns['order_products'] = __( 'Products', 'woocommerce' );
}
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
// Populate the Column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'order_products' ) {
// Get order
$order = wc_get_order( $post_id );
echo '<ul>';
// Going through order items
foreach ( $order->get_items() as $item ) {
// Get product object
$product = $item->get_product();
echo '<li><a href="' . $product->get_permalink() . '">' . $item->get_name() . '</a> × ' . $item->get_quantity() . '</li>';
}
echo '</ul>';
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );
我正在尝试在“订单”页面(管理员)上显示一列。在该列中,显示订单中的每个产品。
想法是能够快速查看已订购的产品。
添加列工作正常,但无法获取产品。我得到的只是 Fatal error: Uncaught ArgumentCountError
代码如下:
add_filter( 'manage_edit-shop_order_columns', 'products_column_create_products', 20 );
function products_column_create_products( $columns ) {
$reordered_columns = array();
foreach( $columns as $key => $column ) {
$reordered_columns[$key] = $column;
if ( $key == 'order_number' ) {
$reordered_columns['products'] = __( 'Products','woocommerce' );
}
}
return $reordered_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'products_column_show_products', 20, 3 );
function products_column_show_products($column, $post_id, $order){
if ('products' != $column) return;
global $order;
$details = array();
foreach($order->get_items() as $item)
$details[] = '<a href="' . $item->get_product()->get_permalink() . '">' . $item->get_name() . '</a> × ' . $item->get_quantity() .'<br>';
echo count($details) > 0 ? implode('<br>', $details) : '–';
}
我不知道如何解决,所以如果有人可以提供建议,请告诉我
您的代码有一些小错误,例如:
- 您指出
products_column_show_products()
回调函数需要 3 个参数,而manage_{$post->post_type}_posts_custom_column
WordPress 挂钩包含 2 个参数。$column_name
和$post_id
- 不需要使用
global $order;
,通过$post_id
, 您可以使用wc_get_order( $post_id );
访问订单
所以这应该足够了:
// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
// Loop trough existing columns
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// Add after order status column
if ( $key === 'order_number' ) {
$new_columns['order_products'] = __( 'Products', 'woocommerce' );
}
}
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
// Populate the Column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'order_products' ) {
// Get order
$order = wc_get_order( $post_id );
echo '<ul>';
// Going through order items
foreach ( $order->get_items() as $item ) {
// Get product object
$product = $item->get_product();
echo '<li><a href="' . $product->get_permalink() . '">' . $item->get_name() . '</a> × ' . $item->get_quantity() . '</li>';
}
echo '</ul>';
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );