//获取单个属性
$product = WC_Product::get_instance( $product_id );
$color = $product->get_attribute( 'color' );
//获取所有属性
$product = WC_Product::get_instance( $product_id );
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ) {
echo $attribute['name'] . ': ' . $attribute['value'];
}
转载:https://www.websitebuilderinsider.com/how-do-i-get-attribute-value-in-woocommerce/#:~:text=In%20this%20article%2C%20we%E2%80%99ll%20show%20you%20how%20to,WC_Product%3A%3Aget_attribute%20%28%29%20method%20to%20get%20an%20attribute%20value
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'pa_color', // 替换为你的属性名称
'field' => 'slug',
'terms' => 'red' // 替换为你的属性选项值
),
),
);
$products = new WP_Query( $args );
while ( $products->have_posts() ) {
$products->the_post();
// 显示产品信息
}
wp_reset_query();
$attributes = wc_get_attribute_taxonomies();
if($attributes) {
echo 'Choose';
foreach ( $attributes as $attribute ) {
echo 'attribute_name.'">' . $attribute->attribute_label . '';
}
echo '';
}