1 回答
TA贡献1842条经验 获得超21个赞
我从未使用过 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>';
}
}
}
- 1 回答
- 0 关注
- 296 浏览
添加回答
举报