questions

How can get meta data from WooCommerce product attribute terms

Rate this post

The meta_value of a particular attribute list holds the name of an image. Is there a way I can access the list of meta_values for an attribute? This gives me all the names but I need the meta_values as well:

global $product; 
$stain=$product->get_attribute('pa_stain-color');
echo $stain;

How can I get the meta_values? I’ve tried a number of variants on this but simply cannot get the meta_values.

 

✔️Solution:

As each product attribute is a custom WooCommerce product taxonomy, for a specific custom taxonomy you can get the terms attached to a product and then the term meta data as follows:

$taxonomy = 'pa_stain-color';

$terms    = wp_get_post_terms( get_the_ID(), $taxonomy ); // Get terms for the product

if ( ! empty($terms) ) {
    foreach ( $terms as $term ) {
        $meta_data = get_term_meta( $term->term_id ); // Get all meta data

        // Display the term name
        echo '<p>' . $term->name . '</p>'; 

        // Raw array output from term meta data
        echo '<pre>' . print_r($meta_data, true) . '</pre>'; 
    }
}

Leave a Reply

Your email address will not be published.

Back to top button