0

I've written two functions to show custom fields on the shop pages below each product, which can be viewed here. The ones specifically are Light Bulb Type and Coverage Area. However, these only apply to certain products. Right now, I am typing in N/A when they don't apply, but I would like to write a conditional statement to hide the entire section if the field is left empty.

Unfortunately, every time I do so, the code breaks the site because it is incorrectly written.

These are the functions I've written to show the custom fields.

add_action('woocommerce_shop_loop_item_title','show_coverage');

function show_coverage() {
    echo '<span class="extra-info-head">Coverage Area:</span> ';
    $coverage =  get_post_meta( $post->ID );
    $custom_fields = get_post_custom();
    $coverage = $custom_fields['wpcf-coverage-area'];
    foreach ( $coverage as $key => $value ) {
    echo $value . '<br/>';
}}

add_action('woocommerce_shop_loop_item_title','show_bulbs');

function show_bulbs() {
    echo '<span class="extra-info-head">Light Bulbs:</span> ';
    $custom_fields = get_post_custom();
    $bulbs = $custom_fields['wpcf-light-bulbs'];
    foreach ( $bulbs as $key => $value ) {
    echo $value;
}}

Apologies if these are poorly written and much appreciation to any suggestions and help!

*UPDATE: This was solved with a combination of the answer below and back-end options on WordPress's custom field posts.

4
  • 1
    How are you determining when those fields don't apply? That's not clear from your question. Also, FYI: your code formatting is going to give you nightmares down the road. I'd strongly recommend picking a style and using consistent indentation. As a rule of thumb, if you ever see }} on a line, you're probably doing something unwise. Commented Dec 9, 2016 at 19:36
  • Thank you, Ed! I appreciate your note on the formatting. Some of it is from how it copied over from my text editor giving it inconsistencies, but I understand what you mean. And I'm not currently determining when the fields don't apply. That is what I'm trying to figure out how to do with my current code. All I have written are the functions that allow them to exist. Commented Dec 9, 2016 at 19:57
  • It's hard to know what to tell you here; we need to know how you determine which products have the fields before we can tell you how to show or hide them when appropriate. What do you get if you do var_dump($custom_fields['wpcf-light-bulbs']); for a product with no light bulbs? What about a product that does have light bulbs? Commented Dec 9, 2016 at 20:03
  • There is a text input box on each editable product page within the WooCommerce platform I've installed on WordPress. I've taken a screenshot here of what that looks like (oi64.tinypic.com/6zx2bp.jpg). Right now, if I don't put anything in the input box, the label shows up below the product still, so I've put N/A in those boxes as a temporary fix. Accessories and Replacement Parts are the two categories of items that these will not be applicable to. I apologize if I'm still being confusing. I truly appreciate all of the help and time you've given so far! Commented Dec 9, 2016 at 20:21

1 Answer 1

1

Unfortunately, every time I do so, the code breaks the site because it is incorrectly written.

Your site is erroring out because you have an "undeclared variable."

In the callback function show_coverage(), you are using an undefined variable $post. The function doesn't know about this variable, because you haven't brought it into the function' scope.

While you could include the global $post, it's better to use the API function instead of the global variable.

$coverage = get_post_meta( get_the_ID(), 'wpcf-coverage-area' );

Let's go one step further. What if the custom metadata doesn't exist in the database? Then $coverage is not going to have anything in it. Let's protect for that by checking and then bailing out if nothing is returned.

function show_coverage() {
    $coverage = get_post_meta( get_the_ID(), 'wpcf-coverage-area' );
    if ( ! $coverage ) {
        return;
    }

    echo '<span class="extra-info-head">Coverage Area:</span>';

    foreach ( $coverage as $key => $value ) {
        echo $value . '<br/>';
    }
}

Now, let's do the same treatment to the other callback function:

function show_bulbs() {
    $bulbs = get_post_meta( get_the_ID(), 'wpcf-light-bulbs' );
    if ( ! $bulbs ) {
        return;
    }

    echo '<span class="extra-info-head">Light Bulbs:</span> ';
    foreach ( $bulbs as $key => $value ) {
        echo $value;
    }
}

Now the above functions will fetch the custom field (which is metadata). If it does not exist, you bail out. Otherwise, you start building the HTML markup and rendering it out to the browser.

Sign up to request clarification or add additional context in comments.

5 Comments

I appreciate the assistance! Unfortunately, this has hidden all of the data and does not show anything even if there is information submitted in both wpcf-light-bulbs and/or wpcf-coverage-area. After making some changes to the code, I was able to get Light Bulbs to appear, but the "Light Bulbs:" span still showed up if data wasn't entered. Once again, I do really appreciate your help!
@TaylorCox Sorry, I forgot the not ! operator in the show_coverage function. The code is updated now.
Things are showing up now, but the labels are still appearing even without data. Thank you again!
Then at that point, you need to do some troubleshooting on your own to see (1) does the post have that custom field (metadata) and (2) is there anything in it. You can also look in the wp_postmeta database table. Do a search for wpcf-light-bulbs. Are there posts with that meta key?
Just an update: I was able to solve this by going through the back-end of WordPress and modifying custom field options rather than needing to add additional code beyond what you provided. Once again, thank you so much for your help! I appreciate you going out of your way on this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.