【问题标题】:Display Dokan vendor name and total sales on Woocommerce single product pages在 Woocommerce 单品页面上显示 Dokan 供应商名称和总销售额
【发布时间】:2020-10-13 14:12:10
【问题描述】:

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

但它不起作用。我怎样才能让它发挥作用?

【问题讨论】:

    标签: php wordpress woocommerce vendor dokan


    【解决方案1】:

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

    使用以下短代码:[vendor_shop_name]

    在您的主题的 Snippets 或 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
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      // 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)

      【讨论】:

      • 感谢您的帮助@loictheaztec。但我希望结果显示在这里 - prnt.sc/n47815 而不是在这里 - prnt.sc/n47730。请问你能做到吗?还有如何隐藏图 2 中的供应商地址并在那里显示总销售额?
      • @ladetunjiosibanjo 我没有 Dokan,而且我从未使用过它……我的代码有效,但我不知道如何在特定的 Dokan 部分中显示。您需要为自己找到正确的相关钩子,以将 woocommerce_single_product_summary 替换为正确的钩子。
      【解决方案3】:

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

      我的结果如下所示:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 2017-07-13
        • 2021-12-12
        • 2017-10-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多