[英]How to add a link on woocommerce product page as per the product category?
我能够找到在woocommerce产品页面上添加链接的代码,但我想根据产品类别添加链接。 以下是我添加的用于在产品页面上显示链接的代码。 有人可以帮我获取代码,以根据产品类别将链接添加到产品页面吗?
add_action( 'woocommerce_before_add_to_cart_button',
'content_before_addtocart_button' );
function content_before_addtocart_button() {
echo '<div class="content-section">add custom size <a
href="http://google.com">here</a></div>';
}
将此代码放在您的functions.php文件中,如果在这种情况下类别塞与“夹克”匹配,它将输出一个链接。
add_action( 'woocommerce_before_add_to_cart_button', 'content_before_addtocart_button' );
function content_before_addtocart_button() {
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
if( $term->slug === 'jackets')
echo '<div class="content-section">add custom size <a href="' . esc_url( get_term_link( $term->term_id, 'product_cat' ) ) . '">' . $term->name . '</a></div>';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.