title: How to display WordPress WooCommerce custom attribute in templates/functions?
body: <p>Would put this in WP Stack Exchange, but often they say since it has PHP it should be in SO, so never quite sure where is best. Can move if more appropriate.</p>
  
  <p>To display a custom woocommerce product attribute called, “Fabrics” for example I’ve read you could do the following.</p>
  
  <pre><code>$fabric_values = get_the_terms( $product-&gt;id, ‘pa_fabrics’);
  
  foreach ( $fabric_values as $fabric_value ) {
     echo $fabric_value-&gt;name;
  }
  </code></pre>
  
  <p>However, is there a shorter way since we would be using a lot of attributes throughout php templates. </p>
  
  <p>For instance is there a way to simply do, “echo get_the_terms( $product->id, ‘pa_fabrics’);”</p>
  
  <p>Or is there a single function one could add to their site, so that would then be able to echo any product attribute which a single very short line like above like you can when use “Advanced Custom Fields” with non WooCommerce sites?</p>
  
  <p>UPDATE</p>
  
  <p>Found <a href="https://stackoverflow.com/questions/39394127/woocommerce-display-single-product-attributes-with-shortcodes-in-frontend">this thread</a> on SO that offers a way to create a single short code to relatively easily get the data. While that's certainly an option, would like to see if there is any cleaner built in ways such as:</p>
  
  <pre><code>echo get_the_terms( $product-&gt;id, 'pa_fabrics');
  </code></pre>
  
  <p>or</p>
  
  <pre><code>echo $product-&gt;get_attributes('pa_fabrics');
  </code></pre>
  
  <p>The last option seems the cleanest and most ideal but results in an error: "Fatal error: Uncaught Error: Call to a member function get_attributes() on null in (my functions.php file where the code was added).</p>