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?