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

问题描述

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

基于 Display vendor store-name (Dokan) on WooCommerce admin order preview,我创建了这个代码片段来做到这一点:

// 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 文件中。现在应该可以使用了。