Refer to specific product ID in WordPress / WooCommerce in PHP

I have this script that adds a download button to every product page in WooCommerce but I need to exclude a product page from having the download button.

I tried changing the first line to

Read More
if ( is_product() || $id() != "1514" ){  

but it is failing, I think I am on the right track but need to reference it correctly (It should only enter the if function if is a product AND its not product ID 1514)

if ( is_product()){ 
$link = get_the_title();
$link =  str_replace(' ', '-', $link);
echo '<div class="download">';
echo '<a href="';
echo bloginfo('url');
echo '/pdf/Spec-Sheet-';
echo $link . '.pdf';
echo ' " title="Download Spec Sheet" target="_blank">';
echo 'DOWNLOAD SPEC SHEET';
echo '</a>'; 
echo '</div>'; }

Thanks

Related posts

Leave a Reply

3 comments

  1. the answer is

    if(is_product() && get_the_id() == 1253) {
        //do stuff
    }
    

    works in pre_get_posts – didn’t test in the product template, but if it doesn’t, make sure you are in the loop and it’ll work for sure.

  2. One easy way to hide this would be to just use CSS.

    if the theme is using the body_class() function then the page will have its own body class. Something like .postid-1514 and you can hide it using .postid-1514 .download { display: none; } this will completely hide it.