【问题标题】:Display author meta on WooCommerce product page在 WooCommerce 产品页面上显示作者元数据
【发布时间】:2019-09-05 21:39:14
【问题描述】:

我在我的 WordPress 网站上使用 WooCommerce 产品供应商插件。每个供应商都有自己的供应商资料。我想在每个供应商产品上显示作者元数据,例如 LinkedIn、facebook、wikipedia 等。

社交资料数据存储在常规 WordPress 用户资料中。如果您在 WP 安装上创建新用户,您应该在联系信息表单表下看到 facebook 个人资料 URL、Wikipedia、LinkedIn 等。因此,每个用户只需在他们的 WordPress 个人资料中输入他们的社交个人资料。

我是 PHP 新手,所以可能是一个非常基本的问题。我试图通过大量的跟踪和错误来使我的代码工作。但是,当我尝试从标题中获取用户 ID 时,我的代码陷入了死胡同。

如果我做 var_dump( $vendor_data );我得到了我需要的所有用户信息:

array(1) { [63]=> array(21) { ["notes"]=> string(0) "" ["logo"]=> string(0) "" ["profile"]=> string(0) "" ["email"]=> string(0) "" ["admins"]=> array(1) { [0]=> int(20) } ["commission"]=> int(70) ["commission_type"]=> string(10) "percentage" ["paypal"]=> string(0) "" ["timezone"]=> string(5) "UTC+2" ["enable_bookings"]=> string(2) "no" ["per_product_shipping"]=> string(2) "no" ["instant_payout"]=> string(2) "no" ["term_id"]=> int(63) ["name"]=> string(9) "Tobias M." ["slug"]=> string(8) "tobias-m" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(63) ["taxonomy"]=> string(20) "wcpv_product_vendors" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(2) } }

通过上面的 var_dump,我将第 11 行更改为:

$vendor_data[ $vendor_id ] = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor_id )->name;`

在做了一些研究后,我发现 -> 无效,因为 -> 表示我正在访问一个对象,但就我而言,我需要从数组中检索数据。因此我尝试了这样的事情,但仍然没有运气:

// get vendor data
$vendor_data = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor_id['name'] );
$name = $vendor_data['name'];

我的密码:

add_action( 'woocommerce_after_add_to_cart_button', 'our_vendor_info' );
function our_vendor_info() {
    if( is_product() ) {
        // get the product id of the order item
                $product_id = get_queried_object()->ID;

                // get vendor id from product id
                $vendor_id = WC_Product_Vendors_Utils::get_vendor_id_from_product( $product_id );

                // get vendor data
                $vendor_data[ $vendor_id ] = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor_id )->name;

        var_dump( $vendor_data );

        // Now get user id from title
        $user = get_userdatabylogin($vendor_data);

                      // Get social profiles
              $wikipedia = get_the_author_meta( 'wikipedia', $user->ID );   

        echo '<div class="large-3 columns venprofilelink">';
        if (get_the_author_meta( 'wikipedia', $user->ID )) {
            echo '<span class="some-talent"><a href="'. $wikipedia .'" target="blank" rel="noopener noreferrer"><img src="http://example.local/wp-content/uploads/2019/09/wikipedia-logo.svg" /></a></span>';
        } else {
            echo 'Hello World!';
            }
        }
    echo '</div>';
}

【问题讨论】:

  • 您检查过数据库中是否存在元键“wikipedia”吗?
  • 我在数据库方面并不是很有经验。但是,我确实看了一下,在 wp_usermeta 中有一个名为 meta_key 的列,其中列出了所有社交媒体配置文件:facebook、instagram、linkedin、myspace、pinterest、soundcloud、tumblr、twitter、youtube 和 wikipedia。然后将配置文件链接存储在 meta_values 中。这是常规的 WP 用户元数据。如果您在全新 WordPress 安装上创建新用户,您应该能够在联系信息表单表下看到社交资料。
  • 除非设置了值,否则不会在数据库中创建该条目,但我假设您希望显示的部分或所有用户的值已设置。通常,我想检查是否找到/满足了每个必要和预期的值/条件。所以,首先,你得到的是“Hello World!”吗?在前端?如果不是,则表明 is_tax( 'shop_vendor' ) 为 false 或者 'product_vendors_tab_content_before' 操作未按预期工作。
  • 是的,部分用户的值已经填入并存入数据库。不,我没有收到 Hello World。我试图将 shop_vendor 更改为 product_cat 但这没有任何区别......我在标签内容之前更改为 woocommerce_after_add_to_cart_button 做了一些事情 - 我的布局搞砸了,但仍然没有输出:/
  • is_tax( 'shop_vendor' ) 绝对是错误的。我从我正在链接的博客文章中获取了 if 语句。我看到他正在使用它在商店供应商的后端放置上传字段......我只需要在前端输出而不是商店供应商后端......

标签: php wordpress woocommerce hook


【解决方案1】:

我从来没有使用过 WooCommerce Vendors 插件,所以我没有测试过这段代码。更具体地说,看起来$vendor_id 可能只是与供应商关联的常规用户 ID。如果是这种情况,那么获取用户 ID 的子例程将是不必要的,但是,以防万一,我将包含包含它的代码的第二个版本。我也在重写它以避免输出一个空的 div 如果里面没有东西可以进去。

另外,woocommerce_after_add_to_cart_button 是一个出现在single-product 模板中的钩子,因此您不需要测试您是否使用is_product() 显示产品。

/**
 * Output a Wikipedia image-link for a a Woocommerce Vendor
 * If $vendor_id is equal to $user_id
 **/
add_action( 'woocommerce_after_add_to_cart_button', 'our_vendor_info' );

function our_vendor_info() {

    // get vendor id from product id
    $vendor_id = WC_Product_Vendors_Utils::get_vendor_id_from_product( get_the_ID() );

    if ( $vendor_id && get_the_author_meta( 'wikipedia', $vendor_id ) ) {

        // Get Wikipedia profile
        $wikipedia = get_the_author_meta( 'wikipedia', $vendor_id );   

        echo '<div class="large-3 columns venprofilelink">';

        echo '<span class="some-talent">
            <a href="'. $wikipedia .'" target="blank" rel="noopener noreferrer">
                <img src="/wp-content/uploads/2019/09/wikipedia-logo.svg" />
            </a>
         </span>'; 

        echo '</div>';

    }

}

如果不是这样,$vendor_id 确实是一个唯一的 ID 号,这意味着将其与特定用户 ID 相关联的编码人员不会费心将后者包含在 $vendor_id 数据或其任何实用程序函数中,或者过滤器或操作,那么您将需要一些更复杂的东西,例如您或其他编码员所写的内容:

/**
 * If $vendor_id is *not* equal to $user_id
 **/
add_action( 'woocommerce_after_add_to_cart_button', 'our_vendor_info' );

function our_vendor_info() {

    // get vendor id from product id
    $vendor_id = WC_Product_Vendors_Utils::get_vendor_id_from_product( get_the_ID() );

    //if there's no vendor_id then nothing to do here
    if ( $vendor_id ) {

        // get vendor data so we can get User ID
        $vendor_data = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor_id ); 

        //get_userdatabylogin() is deprecated, also $vendor_data does not include a "login," but does include a slug
        //(which should be reliable)
        $user_id = get_user_by( 'slug', $vendor_data['slug'] )->ID ; 

        //don't output anything unless there's a reason to do it
        if ( get_the_author_meta( 'wikipedia', $user_id ) ) {

            // Get Wikipedia profile url
            $wikipedia = get_the_author_meta( 'wikipedia', $user_id );   

            echo '<div class="large-3 columns venprofilelink">';

            echo '<span class="some-talent">
                <a href="'. $wikipedia .'" target="blank" rel="noopener noreferrer">
                    <img src="/wp-content/uploads/2019/09/wikipedia-logo.svg" />
                </a>
             </span>'; 

            echo '</div>';

        }

    }

}

【讨论】:

  • 以 $vendor_id 作为唯一 ID 的第二个解决方案起到了作用。非常感谢!我真的是一个 php 新手,但在这个过程中学到了很多……你对我指出正确的方向很有帮助。非常感谢!
猜你喜欢
  • 2016-05-07
  • 2020-01-03
  • 2017-06-14
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多