wordpresswoocommercehook-woocommerceordersdokan

Woocommerce Show vendor Details such as Vendor Name, Store Name, Contact Details and Vendor Address (Dokan) in admin order details page


We want to show the Store Name, Vendor name and Vendor Contact Details from an order admin order details page.

I'm using WooCommerce show vendor store-name (Dokan) in admin order details overview answer code to display the Store name for each product in the invoice.

Now we want to display Vendor Name and Vendor Contact details.

Order Page Screenshot

My attempt:

// Adding Vendor Details admin shop_order pages
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
    // Empty array
    $shop_names = array();

    // Output
    echo '<h4>' . __( 'Store Name: ', 'woocommerce' ) . '</h4>';
    
    // Loop through order items
    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();
        
        // OR JUST USE THIS FOR SHOPNAME
        // Shop name
        // $shop_name = dokan()->vendor->get( $author_id )->get_shop_name();
        
        // NOT in array
        if ( ! in_array( $shop_name, $shop_names ) ) {
            // Push to array
            $shop_names[] = $shop_name;

            // Output
            echo '<p>' . $shop_name . '</p>';
        }
        
    }
    
        // Empty array
    $store_phones = array();
    
        // Output
    echo '<h4>' . __( 'Seller Contact: ', 'woocommerce' ) . '</h4>';
    
    // Loop through order items
    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 );
        
        $store_phone = $vendor->get_phone();
        
        // OR JUST USE THIS FOR SHOPNAME
        // Shop name
        // $store_phone = dokan()->vendor->get( $author_id )->get_store_phone();
        
        // NOT in array
        if ( ! in_array( $store_phone, $store_phones ) ) {
            // Push to array
            $store_phone[] = $store_phone;

            // Output
            echo '<p>' . $store_phone . '</p>';
        }
        
    }
}

BY using this code we got store name but we aren't able to get vendor name and vendor contact details.


Solution

  • Your issue in this code $store_phone[] = $store_phone; replace with this $store_phones[] = $store_phone;

    // Adding Vendor Details admin shop_order pages
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );
    function action_woocommerce_admin_order_data_after_billing_address( $order ) {
        // Empty array
        $shop_names = array();
    
        // Output
        echo '<h4>' . __( 'Store Name: ', 'woocommerce' ) . '</h4>';
        
        // Loop through order items
        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();
            
            // OR JUST USE THIS FOR SHOPNAME
            // Shop name
            // $shop_name = dokan()->vendor->get( $author_id )->get_shop_name();
            
            // NOT in array
            if ( ! in_array( $shop_name, $shop_names ) ) {
                // Push to array
                $shop_names[] = $shop_name;
    
                // Output
                echo '<p>' . $shop_name . '</p>';
            }
            
        }
        
            // Empty array
        $store_phones = array();
        
            // Output
        echo '<h4>' . __( 'Seller Contact: ', 'woocommerce' ) . '</h4>';
        
        // Loop through order items
        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 );
            
            $store_phone = $vendor->get_phone();
            
            // OR JUST USE THIS FOR SHOPNAME
            // Shop name
            // $store_phone = dokan()->vendor->get( $author_id )->get_store_phone();
            
            // NOT in array
            if ( ! in_array( $store_phone, $store_phones ) ) {
                // Push to array
                $store_phones[] = $store_phone;
    
                // Output
                echo '<p>' . $store_phone . '</p>';
            }
            
        }
    }
    

    Tested and works

    enter image description here