简体   繁体   English

在WooCommerce产品页面上显示作者元数据

[英]Display author meta on WooCommerce product page

I am using the WooCommerce product Vendor plugin on my WordPress site. 我在WordPress网站上使用了WooCommerce产品供应商插件。 Each vendor got their own vendor profile. 每个供应商都有自己的供应商资料。 I want to display author meta such as LinkedIn, facebook, wikipedia etc. on each vendor product. 我想在每个供应商产品上显示诸如LinkedIn,Facebook,Wikipedia等的作者meta。

The social profiles data is stored in the regular WordPress user profile. 社交资料数据存储在常规WordPress用户资料中。 If you create a new user on a WP installation you should see facebook profile URL, Wikipedia, LinkedIn etc. under the contact information form table. 如果您在WP安装上创建新用户,则应该在联系信息表格表下看到facebook个人资料URL,Wikipedia,LinkedIn等。 Each user therefore just enter their social profiles within their WordPress profile. 因此,每个用户只需在WordPress个人资料中输入其社交资料即可。

I am new to PHP so probally a pretty basic question. 我是PHP新手,所以可能是一个非常基本的问题。 I tried to make my code work by a lot of trail & error. 我试图通过大量的跟踪和错误使我的代码正常工作。 However, I am running into a dead-end in my code when I try to get user id from the title. 但是,当我尝试从标题中获取用户ID时,我的代码陷入了僵局。

If I do var_dump( $vendor_data ); 如果我这样做var_dump($ vendor_data); I get all of the user information I need: 我得到了我需要的所有用户信息:

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

With the var_dump above I've changed line 11 to be: 在上面的var_dump中,我将第11行更改为:

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

After doing some research I found that -> is not valid because -> means I am accessing an object, but in my case, I need to retrieve data from array. 经过一些研究后,我发现->无效,因为->意味着我正在访问一个对象,但就我而言,我需要从数组中检索数据。 Therefore I tried something like this but still no luck: 因此,我尝试了类似的方法,但是还是没有运气:

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

MY CODE: 我的密码:

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

I've never used the WooCommerce Vendors plugin, so I haven't tested this code. 我从未使用过WooCommerce Vendors插件,因此我尚未测试此代码。 More specifically, it looks like $vendor_id may just be a regular User ID associated with a Vendor. 更具体地说,看起来$vendor_id可能只是与供应商关联的常规用户ID。 If that's the case, then the sub-routine for getting the User ID would be unnecessary, but, just in case that's wrong, I'll include a second version of the code that includes it. 如果真是这样,那么获取用户ID的子例程将是不必要的,但是,以防万一这是错误的,我将提供包含它的代码的第二个版本。 I'm also re-writing it to avoid outputting an empty div if there's nothing to go inside it. 我也正在重新编写它,以避免内部无内容时输出空div。

Also, woocommerce_after_add_to_cart_button is a hook that appears in single-product templates, so you shouldn't need to test whether you're displaying a product with is_product() . 另外, 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>';

    }

}

If that's not the case, and $vendor_id really is a unique ID number, meaning the coders who associated it with specific User IDs didn't bother to include the latter in the $vendor_id data or any of their utility functions or filters or actions, then you'll need something a little more complicated, like what you or that other coder wrote: 如果不是这种情况,并且$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>';

        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM