I’m struggling to get a custom field to correctly display on a Woocommerce single product page. Things I know are working:
I know for sure I’m editing the corect section of Woocommerce – I have my override templates in a /woocommerce directory inside my theme and I’ve successfully made other changes (this is my first ACF Pro change).
I know the custom field exists, and is being correctly displayed on the woocommerce product pages in the backend.
I know that the image I’ve associated to this particular custom field (called ‘custom_bg’) is in the media library.
I know the section where I’m trying to add this field is outside the loop.
Things I’ve tried:
<img src="<?php echo get_field('custom_bg', 98); ?>"></img> – (98 is the id of the Woo product) shows nothing
Ok, in way of an update, I’ve done a bit more investigation and discovered the following. Using <?php echo get_field('custom_bg', $product->ID); ?> correctly displays the image from the custom field – YAY!
However, this only works on certain products! Right now, during testing, I only have two active Woocommerce products, with IDs 98 and 106. I’ve added some debug code to the page so I know what the current page ID is at any time. When I visit product 106, the debug code correctly shows 106, and the custom_bg field is displayed. And when I visit product 98 I see 98 in the debug code but no custom_bg field displays. What’s most perplexing is when I visit my Woocommerce Shop page, the debug code shows 98.
Is this expected behaviour? Is the ID clash what’s stopping the custom_bg field from displaying?
I’m going to add another product to see if it displays there…
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users