Skip to content
Prodjex

How to Get a Product's Custom Attributes from WooCommerce

1 min read

How to Get a Product's Custom Attributes from WooCommerce

Pulling a product's custom attributes in WooCommerce is a simple task using the get_attribute() function.

Prior to WooCommerce version 3 we would use the woocommerce_get_product_terms like so.

global $product;
$productAttribute = array_shift(woocommerce_get_product_terms($product->id, 'pa_myCustomAttribute', 'name'));

The woocommerce_get_product_terms has been deprecated as of WooCommerce 3.  The new method to pull the custom attributes can be done a couple of different ways.  The simplest method in our opinion is the following.

global $product;
$productAttribute = $product->get_attribute( 'pa_myCustomAttribute' );

If you have multiple fields for the attribute you can call it with a little more code:

global $product;
$productAttribute = array_shift( wc_get_product_terms( $product->id, 'pa_myCustomAttribute', array( 'fields' => 'name' ) ) );
  • attributes
  • custom
  • custom attributes
  • custom meta
  • meta
  • meta data
  • products
  • woocommerce
  • wordpress

Keep reading