woocommerce get list of attribute values

前端 未结 4 897

I\'m using woocommerce on wordpress to create a simple shop site and I\'ve added a couple attributes to a product. These are namely, size and color. Un

相关标签:
4条回答
  • 2021-02-04 08:59

    You can use get_terms() http://codex.wordpress.org/Function_Reference/get_terms

    If you pass in pa_size or pa_color you will get back a list of terms in that taxonomy.

    0 讨论(0)
  • 2021-02-04 09:04

    This post was written some time ago so I don't know if Woocommerce had this method in its previous incarnations.
    For anyone else looking to do this, this line is all you need.

    $product->list_attributes();
    

    This allows you to customize the order and toggle whether or not you want to display the variation in the backend,

    0 讨论(0)
  • 2021-02-04 09:14

    Hoping this is helpful to someone:

    global $product; 
    
    // Get product attributes
    $attributes = $product->get_attributes();
    
    if ( ! $attributes ) {
        echo "No attributes";
    }
    
    foreach ( $attributes as $attribute ) {
    
            echo $attribute['name'] . ": ";
            $product_attributes = array();
            $product_attributes = explode('|',$attribute['value']);
    
            $attributes_dropdown = '<select>';
    
            foreach ( $product_attributes as $pa ) {
                $attributes_dropdown .= '<option value="' . $pa . '">' . $pa . '</option>';
            }
    
            $attributes_dropdown .= '</select>';
    
            echo $attributes_dropdown;
    }
    
    0 讨论(0)
  • 2021-02-04 09:16

    In addition to @user5029040 answer, which outputs html, if you want to get an array you can use the following function.

    $product->get_variation_attributes();
    
    0 讨论(0)
提交回复
热议问题