在WooCommerce订单列表中显示Dokan自定义订单元数据



对于WooCommerce,我使用Dokan Multivendor,并希望在一列中显示订单的供应商商店名称。默认情况下,Dokan显示订单的作者名称,而不是商店名称。

基于WooCommerce管理订单预览上显示供应商商店名称(Dokan(,我创建了以下代码片段:

// Create column
function wc_new_order_column( $columns ) {
$columns['vendor_store_name_order'] = __( 'Vendor'); 
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'wc_new_order_column' );
// Display vendor or vendors of the order
function action_woocommerce_admin_order_vendor( $order ) {
foreach ( $order->get_items() as $item ) {

// Get product object
$product = $item->get_product();

// Author id
$author_id = $product->post->post_author;

// Shopname
$vendor = dokan()->vendor->get( $author_id );
$shop_name = $vendor->get_shop_name();

// NOT in array
if ( ! in_array( $shop_name, $shop_names ) ) {

// Push to array
$shop_names[] = $shop_name;
// Filter URL for other orders from same vendor => When click, show all other orders from that vendor
$vendor_orders = get_admin_url() . 'edit.php?post_type=shop_order&vendor_id=' . $author_id;

// Column Output
if ( $column == 'vendor_store_name_order' ) {
printf('<a href="%s">%s</a>', $vendor_orders, $shop_name);      
}           
}
}
}
add_action( 'manage_shop_order_posts_custom_column', 'action_woocommerce_admin_order_vendor', 10, 2 );

目前该代码不起作用。订单项foreach循环出现问题。我想访问订单,然后检查其中的项目,以检查它是哪个供应商。然后用数组显示每个在该订单中有产品的供应商。

但由于每个订单的当前状态,我无法以正确的方式访问订单信息。我必须如何访问它?

您的代码中存在一些错误。它可以简化和优化如下:

// Add a new column to admin orders list
add_filter( 'manage_edit-shop_order_columns', 'wc_new_order_column' );
function wc_new_order_column( $columns ) {
$columns['store_name'] = __( 'Vendor'); 
return $columns;
}
// Display the linked vendor(s) shop names(s) for each order in the custom columns
add_action( 'manage_shop_order_posts_custom_column', 'action_woocommerce_admin_order_vendor', 10, 2 );
function action_woocommerce_admin_order_vendor( $column ) {
if ( 'store_name' === $column ) {
global $post, $the_order;

$order = is_a($the_order, 'WC_Order') ? $the_order : wc_get_order( $post->ID );
$link  = get_admin_url() . 'edit.php?post_type=shop_order&vendor_id=';
$data  = array(); // Initializing
foreach ( $order->get_items() as $item ) {
$authid = get_post_field( 'post_author', $item->get_product_id() );

if ( ! in_array( $authid, array_keys($data) ) ) {
$vendor        = dokan()->vendor->get($authid);
$shop_name     = $vendor->get_shop_name();
$data[$authid] = '<a href="'.$link.$authid.'">'.$shop_name.'</a>';
}
}
echo implode('<br>', $data); // Output
}
}

代码位于活动子主题(或活动主题(的functions.php文件中。现在应该可以了。

最新更新