简体   繁体   中英

How to get meta data from WooCommerce product attribute terms

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.

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>'; 
    }
}

Documentation: WordPress get_term_meta() function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM