在Woocommerce单一产品页面上显示Dokan供应商名称和总销售额

时间:2019-03-26 19:04:58

标签: php wordpress woocommerce vendor dokan

使用Woocommerce和Dokan插件。我试图在单个产品页面的“供应商信息”选项卡上显示供应商名称和总销售额。

基于Display dokan vendor name on Woocommerce single product pages)” 问题,我尝试使用以下代码来显示供应商名称和总销售额:

//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'] );
}

但是它不起作用。我该如何运作?

3 个答案:

答案 0 :(得分:0)

以下应在单个产品页面上显示供应商名称和总销售额:

// 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" )
    );
}

代码进入您的活动子主题(或活动主题)的function.php文件中。现在应该可以使用。


有关“自定义产品”标签,请参见Editing product data tabs in Woocommerce (official docs and code)

答案 1 :(得分:0)

我正在使用dokan插件。我想在侧边栏显示供应商信息购物车。我使用了您的代码并进行了一些不专业的更新。在下面的代码中。但我想显示有关供应商的更多信息,例如徽标,等级,供应商页面的按钮等。

这是我的结果:

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
    }
}

答案 2 :(得分:0)

对于那些只想使用短代码链接到Dokan商店的人来说,这是Loic方法的简化版本。您可以在单个产品页面上使用它。

使用以下短代码:[vendor_shop_name]

在您的主题的代码片段或functions.php中输入以下代码。 Functions.php可以在主题文件中找到,即。 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
    }
}
相关问题