2
votes

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.

1
1) If you are using existing code, please always cite the source, because it gives proper credit to the author. 2) Why would you use 2 foreach loops (and slow things down) when you can do this ALL with 1 loop?7uc1f3r

1 Answers

1
votes

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