Home › Forums › Front-end Issues › WooCommerce Attribute Image to Single Product
Hi,
I have created an ACF field called Finish Image 2 that adds a image field to a product attribute. I can not find a way to get this image to show up on the single product page. I am new to ACF, how do I get this image to show on this page? All I know is that I probably need to use code like
get_field( ‘finish_image_2’ );
Thank you for all your help, wish I had more information for you.
When working with WC you more than likely need to use one of their action hooks to show the field where you want it shown on the product page. https://docs.woocommerce.com/wc-apidocs/hook-docs.html
In your action you need to get/show the value by passing ACF the product ID (Post ID), example: get_field('your-field-name', $product_id);
Some of the action hooks pass the product ID and in other cases you’ll need to figure out how to get the ID.
Well I have done some research and have almost got the code working. Currently the code looks like this
add_action(‘woocommerce_single_product_summary’,”finish_image”,22);
function finish_image() {
echo ‘<b>Finish:</b>’ .get_field(‘finish_img’, $product_id);
}
For some reason the image will not show, It just shows the text “array”(not the setting the actual word array). If I change it to URL, It shows the right url and if I change it to Image ID it show the right id.
After playing with the settings more, I can no longer get anything to come up, please help!
Thanks
Jake
There are choices for what to return when using an image field. See the image field doc and how to use it https://www.advancedcustomfields.com/resources/image/
The thing is even if I change it to a text box, not data can been seen now. Not sure what I did, but I think I broke ACF…. I can tell that its going to go to the right place because I can see the word Finish: from the echo. Does my code look right to you?
I have gotten the image url to show back up if I give the full path to a single Attribute.
add_action(‘woocommerce_single_product_summary’,”finish_image”,22);
function finish_image() {
echo ‘<b>Finish:</b>’. get_field(‘finish_img’, ‘pa_metal-finish_79’);
}
But I need the code to search though the metal finish attributes to find the one that applies to that Item.
I know its something like the post found here.
https://support.advancedcustomfields.com/forums/topic/get-field-from-woocommerce-attribute-taxonomy/
but I cant seem to quite figure it out, do you have any insite?
Making progress I know have the code working, only shows url still though. need to look though the document above
add_action(‘woocommerce_single_product_summary’,”finish_image”,22);
function finish_image() {
$terms = get_the_terms( $post->ID , ‘pa_metal-finish’ );
if($terms) {
foreach( $terms as $term ) {
echo ‘<b>Finish:</b>’. get_field(‘finish_img’, ‘pa_metal-finish’. ‘_’ .$term->term_id);
}
}
}
I am close though
I am so close, keep coming up with a parce error. What am I doing wrong here? I know it has something to do with the IMG SRC and quotes
add_action(‘woocommerce_single_product_summary’,”finish_image”,22);
function finish_image() {
$terms = get_the_terms( $post->ID , ‘pa_metal-finish’ );
if($terms) {
foreach( $terms as $term ) {
echo ‘<b>Finish:</b>’. ‘term_id);’ ?>’ />’
}
}
}
Sorry I just realized I should have added it as a code
add_action('woocommerce_single_product_summary',"finish_image",22);
function finish_image() {
$terms = get_the_terms( $post->ID , 'pa_metal-finish' );
if($terms) {
foreach( $terms as $term ) {
echo '<b>Finish:</b>'. '<img src= '<?php get_field('finish_img', 'pa_metal-finish'. '_' .$term->term_id);' ?>' />'
}
}
}
I got it working using the Wysiwyg Editor to upload the image instead. Thanks for your help.
The final code is
add_action('woocommerce_single_product_summary','finish_image',22);
function finish_image() {
$terms = get_the_terms( $post->ID , 'pa_metal-finish' );
if($terms) {
foreach( $terms as $term ) {
echo '<b>Finish: </b>'. get_field('finish_img', 'pa_metal-finish'.'_'.$term->term_id);
}
}
}
With use of the Wysiwyg Editor for image upload
The topic ‘WooCommerce Attribute Image to Single Product’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.