[英]Woocommerce: programmatically add links if products has a tribute value
I am trying to add a link to a product if it has a certain attribute value.如果产品具有特定属性值,我正在尝试添加指向产品的链接。 This is what I have这就是我所拥有的
function stilart_link() {
echo '<p class="stilart">Mere i samme stil: </p>';
$terms = get_the_terms( $product->id, 'pa_stilart');
foreach($terms as $term){
if($term->name == 'Rock'){
echo '<p class="stilart"><a href="/stilart/rock"> <u>Rock-noder</u></a></p>';
}
else if($term->name == 'Musical'){
echo '<p class="stilart"><a href="/stilart/musical"> <u>Musical-noder</u></a></p>';
}
else if($term->name == 'Disney'){
echo '<p class="stilart"><a href="/stilart/disney"> <u>Disney-noder</u></a></p>';
}
else if($term->name == 'Filmmusik'){
echo '<p class="stilart"><a href="/stilart/filmmusik"> <u>Filmmusik</u></a></p>';
}
else if($term->name == 'Jul'){
echo '<p class="stilart"><a href="/stilart/jul"> <u>Julenoder</u></a></p>';
}
}
}
add_action( 'woocommerce_after_single_product_summary', 'stilart_link' );
It does work.它确实有效。 But I wonder if it could be done automatically to avoid long code.但我想知道是否可以自动完成以避免长代码。 This is what I am looking for: a product has a value that belongs to the tribute "stilart".这就是我要寻找的:一种产品具有属于致敬“stilart”的价值。 Eg.例如。 "Disney". “迪士尼”。 On that product I want to add a link to /stilart/disney.在该产品上,我想添加指向 /stilart/disney 的链接。 And the same procedure with other values of the tribute "stilart – eg. Muscal, filmmusik.与其他价值观的致敬“stilart – 例如Muscal,filmmusik。
You can use slug and title property that came from $term object.您可以使用来自 $term object 的 slug 和 title 属性。 Try like this:试试这样:
function stilart_link() {
echo '<p class="stilart">Mere i samme stil: </p>';
$terms = get_the_terms( $product->id, 'pa_stilart');
foreach($terms as $term){
echo '<p class="stilart"><a href="/stilart/'.$term->slug.'"> <u>'.$term->name.'</u></a></p>';
}
}
add_action( 'woocommerce_after_single_product_summary', 'stilart_link' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.