1 回答

TA貢獻1842條經(jīng)驗 獲得超22個贊
我從未使用過 WooCommerce Vendors 插件,所以我沒有測試過這段代碼。更具體地說,它看起來$vendor_id可能只是與供應(yīng)商關(guān)聯(lián)的常規(guī)用戶 ID。如果是這種情況,那么用于獲取用戶 ID 的子例程將是不必要的,但是,以防萬一這是錯誤的,我將包含包含它的代碼的第二個版本。我也在重寫它以避免在里面沒有任何東西時輸出一個空的 div。
此外,woocommerce_after_add_to_cart_button是一個出現(xiàn)在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 相關(guān)聯(lián)的編碼人員不必費心將后者包含在 $vendor_id 數(shù)據(jù)或其任何實用程序功能或過濾器或操作中,那么您需要一些更復(fù)雜的東西,比如你或其他編碼員寫的:
/**
* 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>';
}
}
}
- 1 回答
- 0 關(guān)注
- 325 瀏覽
添加回答
舉報