2
votes

Working with Woocommerce and Dokan plugin. I am trying to display the vendor name and total sales on the vendor info tab (for customer confidence) on single product pages.

Based on "Display dokan vendor name on Woocommerce single product pages)" question thread, I tried this code to display the vendor name and total sales:

//show vendor total sales
add_action( 'woocommerce_single_product_summary', 'show_total_sales', 20 );
function show_total_sales() {    
    printf( '<b>Seller Name:</b> <a href="%s">%s</a>', dokan_get_store_url( $author->ID ), $store_info['total_sales'] );
}

But it doesn't work. How can I make it work?

3

3 Answers

0
votes

The following should display the vendor name and total sales on single product pages:

// Display vendor total sales
add_action( 'woocommerce_single_product_summary', 'show_total_sales', 20 );
function show_total_sales() {
    global $product;

    $vendor_id   = get_post_field( 'post_author', $product->get_id() ); // Get the author ID (the vendor ID)
    $vendor      = new WP_User($vendor_id); // Get the WP_User object (the vendor) from author ID

    $store_url   = dokan_get_store_url( $vendor_id );  // Get the store URL
    $store_info  = dokan_get_store_info( $vendor_id ); // Get the store data
    $store_name  = $store_info['store_name'];          // Get the store name

    // Output display
    printf( '<p><strong>%s:</strong> <a href="%s">%s (%s %s)</a></p>', 
        __("Seller Name", "woocommerce"), 
        dokan_get_store_url( $vendor_id ),
        $vendor->display_name,
        $store_info['total_sales'],
        _n( "sale", "sales", $store_info['total_sales'], "woocommerce" )
    );
}

Code goes in function.php file of your active child theme (or active theme). This should work now.


For custom product tab see Editing product data tabs in Woocommerce (official docs and code)

0
votes

I am using the dokan plugin. I wanna show the vendor info cart to the sidebar. I used your code and make some unpro update. It is in the below code. But I want to display more info about vendors like logo, ratings, button to vendor page, etc.

Here is how my result looks like:

Screenshot fragment

/**
 * Show sold by on single product page made with Elementor
 * Add the shortcode [dokan_vendor_name] through a short-code widget on single product page
 */
add_shortcode('dokan_vendor_name', 'dokan_store_name_shortcode');

function dokan_store_name_shortcode() {
    global $product;
    $seller = get_post_field('post_author', $product->get_id());
    $author  = get_user_by('id', $seller);
    $vendor = dokan()->vendor->get($seller);
    
    $store_info = dokan_get_store_info($author->ID);
    
    if (!empty($store_info['store_name'])) {
        ?>
        <span class="details">
            <?php printf('**<table style="height: 29px; background-color:  #ccffff; margin-left: auto; margin-right: auto;" width="295"> <tbody> <tr> <td style="width: 285px; text-align: center; "><span style="color: #000000; "><strong>**Sold by:**</strong> <br><a href=" %s">%s</a><br>**    </span></td> </tr> </tbody> </table> ', $vendor->get_shop_url(), $vendor->get_shop_name()); ?>
        </span>
        <?php
    }
}
0
votes

Here's a lighter version of Loic's method for those that just want a shortcode for a link towards the Dokan Store. You can use this on the single product page.

Use the following shortcode: [vendor_shop_name]

Enter Following code in Snippets or functions.php of your theme. Functions.php can be found in theme files ie. html/wp-content/themes/[your_theme]/functions.php

/*Shortcode [vendor_shop_name]*/
add_shortcode('vendor_shop_name', 'vendor_shop_name_function');
function vendor_shop_name_function() {
    global $product;
    $seller = get_post_field('post_author', $product->get_id());
    $author  = get_user_by('id', $seller);
    $vendor = dokan()->vendor->get($seller);
    $store_info = dokan_get_store_info($author->ID);
    
    if (!empty($store_info['store_name'])) {
        ?>
            <?php printf('<span><a href=" %s">%s</a></span>', $vendor->get_shop_url(), $vendor->get_shop_name()); ?>
        <?php
    }
}