How to echo a attribute label in woocommerce?

时光怂恿深爱的人放手 提交于 2021-01-28 19:20:33

问题


Im trying to echo the products attribute labels inside a class in a woocommerce loop. I have tried:

class="<?php echo wc_attribute_label( $name ); ?>"

but it doesn't work. What am I doing wrong?

Thanks for any help possible!

Kind regards, Richard


回答1:


Thanks to Kharis Sulistiyono i got this answer in the wordpress/woocommerce forums:

<?php
global $product;
$attribute = $product->get_attributes();

$attribute_arr = array();

if( count($attribute) > 0 ){
    foreach ($attribute as $key => $value) {
        $attribute_arr[] = $key;
    }
}

$attributes = implode($attribute_arr, ' ');
?>

/* Print as class value */
class="<?php echo esc_attr($attributes); ?>";

It works like a charm!




回答2:


You can do this:

$term = get_term_by( 'name', $variation_product->get_attribute( 'pa_color' ), 'pa_color' );

This gives you a term object, so if you for instance want the labels slug you can just:

echo $term->slug;

Or ID:

echo $term->id; 


来源:https://stackoverflow.com/questions/31516096/how-to-echo-a-attribute-label-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!