简体   繁体   中英

Can't display custom product attribute data on Single Product page with ACF and WooCommerce

I have created a product attribute, and have added custom fields with ACF on that product attribute however I am having trouble displaying that custom field on the single product page.

<?php

function brandLogo(){
    
    $terms = get_terms( 'brand' , array( 'hide_empty' => false ) );
    
    foreach( $terms as $term ) :
    
    $thumbnail_image = get_field( 'brand_logo', 'brand' . $term->term_id );
    
    ?><img src="<?php echo $thumbnail_image ?>"> <?php
    
    endforeach;
    
}

add_shortcode('brandLogo', 'brandLogo');

//$terms = get_the_terms($product->id, 'brand');
//$brandLogo = get_field('brand_logo', $term->taxonomy . '_' . $term->term_id );

?>

The tag shows on the front end but returns null. My attribute is called 'Brand' and my custom attribute is 'brand_logo'. I'm struggling to make sense of any documentation around this area.

Can anyone suggest changes to my code to display a custom field on a product attribute on my single product page?

I changed the code around and learned that it is important to use 'pa_attributename' for WooCommerce product attributes. Here is the working code:

<?php

function brandLogo(){
    
    global $product;
    
    $terms = get_the_terms($post->ID , 'pa_brand');
    
    if($terms){
        foreach ($terms as $term){
            
                ?><img src="<?php the_field('brand_logo', 'pa_brand' . '_' . $term->term_id) ?>"/><?php
        }
    }
    
}

add_shortcode('brandLogo', 'brandLogo');

?>

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