How To Get Custom Product Attributes In WooCommerce

Alpha Insights

Boost Your ROAS.

Connecting WooCommerce Orders To Your Ad Campaigns Is Just The Beginning...

WooCommerce Cost Of Goods Order Profit Reports

How To Get Custom Product Attributes With WooCommerce

You will need to have access to the $product object (or $product_id) to access the custom attributes.

You can either loop through all attributes to find all of the attributes or you can search for a specific one.

Access A Specific Custom Product Attribute's Value

				
					global $product;
$custom_attribute = $product->get_attribute( 'your_custom_attribute' );
echo $custom_attribute;
				
			

The snippet above will grab the value as it is and return it as a string.

If you are checking a  product which has a series of values separated by | pipes | then it will return the full string with pipes included.

For example, it will output value 1 | value 2 | value 3.

If you are deliberately wanting to work with data in this way you could use php explode to combine them into an array and then work with them individually.

Access All Product Attribute's And Their Data

				
						global $product;

	// Get all attributes and their data
	$attributes = $product->get_attributes();

	foreach ( $attributes as $attribute ) {
		
		if ( is_object($attribute) ) {

			// Array of attribute data
			$attribute_data = $attribute->get_data();

			// Do what you need to do...

		}

	}
				
			

If you did a dump of one of the $attribute_data payloads in the loop above you would get an output with something like the below.

This would give you access to all the attribute data you might need to work with.

Array
(
    [id] => 0
    [name] => test
    [options] => Array
        (
            [0] => test_value
            [1] => test_value2
        )

    [position] => 0
    [visible] => 
    [variation] => 
    [is_visible] => 0
    [is_variation] => 0
    [is_taxonomy] => 0
    [value] => test_value | test_value2
)
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Roman
Roman
2 years ago

Fix please:

global $product;

Roman!

Alpha Insights

Alpha Insights

The World's Most Advanced WooCommerce Reporting Plugin.

WooCommerce Cost Of Goods Order Profit Reports