Search
Close this search box.

WORDPRESS获取产品单个属性,和所有属性

//获取单个属性
$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

《WORDPRESS获取产品单个属性,和所有属性》有2条评论

  1. $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();

    回复
  2. $attributes = wc_get_attribute_taxonomies();
    if($attributes) {
    echo 'Choose';
    foreach ( $attributes as $attribute ) {
    echo 'attribute_name.'">' . $attribute->attribute_label . '';
    }
    echo '';
    }

    回复

发表评论

Optimized by WPJAM Basic