With WooCommerce, I use Dokan Multivendor and want to display the vendor store name of the order in a column. By default Dokan displays the author name of the order instead the store name.
Based on Display vendor store-name (Dokan) on WooCommerce admin order preview, I created this code snippet to do that:
// 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 );
Currently the code isn't working. There is an issue with order items foreach loop. I want to access the order and then check for the items in it to check what vendor it is. Then display with the array each vendor which has product in that order.
But with the current for each, I'm not able to access the order infos in the right way. How do I have to access this?
There are some mistakes in your code. It can be simplified and optimized as follows:
// 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
}
}
Code goes in functions.php file of the active child theme (or active theme). It should work now.