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.
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.
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 )
Supercharge your WooCommerce store with WP Davies
Copyright © 2023 Design by WP Davies
ABN 23 988 145 173 | WP DAVIES
Fix please:
Roman!
Good grab Roman!