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

Php 在WooCommerce订单列表中显示Dokan自定义订单元数据,php,wordpress,woocommerce,orders,dokan,Php,Wordpress,Woocommerce,Orders,Dokan,对于WooCommerce,我使用Dokan Multivendor,并希望在列中显示订单的供应商商店名称。默认情况下,Dokan显示订单的作者名称,而不是商店名称 基于,我创建了以下代码段: // Create column function wc_new_order_column( $columns ) { $columns['vendor_store_name_order'] = __( 'Vendor'); return $columns; } add_filter(

对于WooCommerce,我使用Dokan Multivendor,并希望在列中显示订单的供应商商店名称。默认情况下,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 );
//创建列
函数wc\u new\u order\u列($columns){
$columns['vendor\u store\u name\u order']=\u('vendor');
返回$columns;
}
添加过滤器('manage_edit-shop_order_columns'、'wc_new_order_column');
//显示订单的一个或多个供应商
功能操作\商业\管理\订单\供应商($order){
foreach($order->get_items()作为$item){
//获取产品对象
$product=$item->get_product();
//作者id
$author\u id=$product->post->post\u author;
//店名
$vendor=dokan()->vendor->get($author\u id);
$shop_name=$vendor->get_shop_name();
//不在阵列中
if(!in_数组($shop_name,$shop_name)){
//推送阵列
$shop_name[]=$shop_name;
//来自同一供应商的其他订单的筛选URL=>单击时,显示来自该供应商的所有其他订单
$vendor\u orders=get\u admin\u url()。'edit.php?post\u type=shop\u order&vendor\u id='。$author\u id;
//列输出
如果($column=='vendor\u store\u name\u order'){
printf(“”,$vendor\u orders,$shop\u name);
}           
}
}
}
添加操作('manage_shop_order_posts_custom_column','action_woodcommerce_admin_order_vendor',10,2);
目前代码不起作用。每个循环的订单项存在问题。我想访问订单,然后检查其中的项目,以检查它是什么供应商。然后用数组显示按该顺序拥有产品的每个供应商


但由于当前的for each,我无法以正确的方式访问订单信息。如何访问此文件?

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

// 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
    }
}
//将新列添加到管理订单列表
添加过滤器('manage_edit-shop_order_columns'、'wc_new_order_column');
函数wc\u new\u order\u列($columns){
$columns['store_name']=__('Vendor');
返回$columns;
}
//在自定义列中显示每个订单的链接供应商店铺名称
添加操作('manage_shop_order_posts_custom_column','action_woodcommerce_admin_order_vendor',10,2);
功能操作\商业\管理\订单\供应商($column){
if('store_name'=$column){
全球$post,$U订单;
$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();//正在初始化
foreach($order->get_items()作为$item){
$authid=get_post_字段('post_author',$item->get_product_id());
如果(!在数组中($authid,数组密钥($data))){
$vendor=dokan()->vendor->get($authid);
$shop_name=$vendor->get_shop_name();
$data[$authid]='';
}
}
回波内爆(“
”,$data);//输出 } }

代码进入活动子主题(或活动主题)的functions.php文件。现在应该可以用了。

非常感谢。它起作用了!但第14行和第21行中有一些错误,带有“)”。我为if添加了a),并为$order删除了一个“)”variable@Nik7啊!已经很晚了,我眼中的恐惧……对不起:)