Home › Forums › General Issues › Add ACF field of single product page
Hi,
I create a file type field with name pdf.
It shows normally on single product page backend where i can upload a pdf.
How i could show it to front end of single product page on this position: woocommerce_product_thumbnails or this: woocommerce_before_add_to_cart_form ?
I would like to know the code in order to add it to functions.php.
Is it possible to appear only if there is upload pdf in product page?
For example show this: Download the Flibook (with link to pdf) else if it is empty don’t show it.
Thank you!
Hi @danis44
You can try something like:
add_action( 'woocommerce_before_add_to_cart_form', 'my_pdf_field' );
function my_pdf_field() {
$pdf = get_field( 'pdf' );
#if we have data, show it
if( $pdf ){
echo '<a href="'.$pdf['url'].'">link</a>';
}
}
I’ve assumed the PDF field you’ve added is called pdf, it’s a file type and returns an array (not a URL)
Code is untested and goes in your functions.php file
As a quick after thought, you *may* need to access the product ID to get the data from the field, if so, try:
add_action( 'woocommerce_before_add_to_cart_form', 'my_pdf_field' );
function my_pdf_field() {
global $product;
$product_id = $product->get_id();
$pdf = get_field( 'pdf', $product_id );
#if we have data, show it
if( $pdf ){
echo '<a href="'.$pdf['url'].'">link</a>';
}
}
Hi Jarvis and thanks for your code.
It seems to work. I guess it will be shown only if there is file uploaded correct?
Also can i style with css the text?
Also can be open in new tab?
Thanks again!
Hi @danis44
Yes, only works if a pdf has been added, so add a pdf just to check the code works.
echo '<a href="'.$pdf['url'].'" class="your-class-name" target="_blank">link</a>';
Update to this line for a class and opening in a new tab. Just change the class to what you need, then add styling via CSS
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
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.