0

I've created a custom field in Woocommerce to link to games that have the same title but are on different platforms. It basically shows Also available on: XBox One or PS4. It works well but when the field is empty it shows only Also available on: which looks sloppy and is confusing.

How can I hide the custom field when it's empty? The code is below and the custom field is also_available. I've placed it in my functions.php

add_action( 'woocommerce_single_product_summary', 'also_available_on_ps4', 38 ); 
function also_available_on_ps4() {
    global $product;
    if ( has_term( 'ps4-games', 'product_cat' ) ) {
    echo '<b>Also available on:</b>' . get_post_meta( get_the_ID(), 'also_available', true );
    }
}

I hope someone can help me. TIA

0

I'm assuming what you want is to hide the text when meta is empty. You can put a conditional check for meta before print:

add_action( 'woocommerce_single_product_summary', 'also_available_on_ps4', 38 ); 
function also_available_on_ps4() {
    global $product;
    if ( has_term( 'ps4-games', 'product_cat' ) ) {
        if(get_post_meta( get_the_ID(), 'also_available', true )){
            echo '<b>Also available on:</b>' . get_post_meta( get_the_ID(), 'also_available', true );
        }
    }
}
  • Awesome @Aniruddha Gawade. Works perfectly. I could kiss you. – John Cook Jun 30 '18 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.