skip to Main Content

I have created 5 custom meta values for my products on WC but not every product have all custom fields.

At the moment, I am displaying the meta data like so:

<div class="meta">
  <?php if($product->get_meta('metabox_animal') != '') echo '<div class="type"><p>Row One</p></div>' . $product->get_meta('metabox_animal'); ?>
  <?php if($product->get_meta('metabox_medical') != '') echo '<div class="type"><p>Row Two</p></div>' . $product->get_meta('metabox_medical'); ?>
  <?php if($product->get_meta('metabox_general') != '') echo '<div class="type"><p>Row Three</p></div>' . $product->get_meta('metabox_general'); ?>
  <?php if($product->get_meta('metabox_capacity') != '') echo '<div class="type"><p>Row Four</p></div>' . $product->get_meta('metabox_capacity'); ?>
  <?php if($product->get_meta('metabox_pet') != '') echo '<div class="type"><p>Row Five</p></div>' . $product->get_meta('metabox_pet'); ?>
</div>

Is there a way that I can create a loop that will cycle through all of the meta values and if they exist, display them but if they’re blank / empty / not used show a container ‘NOT APPLICABLE’?

Any help would be greatly appreciated!

3

Answers


  1. This not tested and only one way you might get this done:

    <?php
      $html = '';
    
      // collection of meta keys and their display label
      $meta_keys_with_labels = [
        ['animal', 'Row One'],
        ['medical', 'Row Two'],
        ['general', 'Row Three'],
        ['capacity', 'Row Four'],
        ['pet', 'Row Five']
      ];
    
      $html .= '<div class="meta">';
      
      // loop to check if meta key/value exists and append to html string
      foreach ($meta_keys_with_labels as $meta) {
        // unpack the key and display label
        [$key, $label] = $meta;
        
        // grab meta value
        $meta_value = $product->get_meta("metabox_$key");
    
        // append meta value to html string
        if (!empty($meta_value)) {
          $html .= "<div class="type"><p>$label</p></div>$meta_value";
        }
      }
    
      $html .= '</div>';
    
      echo $html;
    
    Login or Signup to reply.
  2. Sure!

    You can create an array of all the meta terms that you need to retrieve.

    Following your code this is the code you will need. Of course you can add/remove as you see fit.

    <?php
    $meta_terms = [
        'metabox_animal',
        'metabox_medical',
        'metabox_general',
        'metabox_capacity',
        'metabox_pet'
    ];
    
    foreach ($meta_terms as $meta_term) {
        $meta = $product->get_meta($meta_term);
    
        if (!empty($meta)) {
            echo '<div class="type"><p>' . $meta . '</p></div>';
        }
    }
    

    So… what the hell did we do here?

    First we created an array of all the meta data that we need to retrieve.

    After we created that array we need to start an iteration to get, check and display each meta data.

    For that we use a foreach (PHP foreach).

    ok

    In the foreach block, using the variable $meta_term that contains the meta string that we need to retrieve we now use it to get the product meta data.

    Now we check if the meta data is not empty, using the !empty() function, if it’s not empty than we know that there is value we can output so, using echo we can now build the proper html.

    Hope this help =D

    Login or Signup to reply.
  3. You can set your related data in an array, then use a foreach loop as follow and if a custom field value is empty or doesn’t exist, "NOT APPLICABLE" will be displayed instead (code is commented):

    <?php 
    // Data array with Meta key / Label pairs
    $data = array(
        'metabox_animal'   => __("Row One"),
        'metabox_medical'  => __("Row Two"),
        'metabox_general'  => __("Row Three"),
        'metabox_capacity' => __("Row Four"),
        'metabox_pet'      => __("Row Five"),
    ); 
    
    echo '<div class="meta">';
    
    // Loop through the data array of Meta key / Label pairs
    foreach ( $data as $meta_key => $label ) {
        $meta_value = $product->get_meta($meta_key); // Get the meta value
        $meta_value = empty($meta_value) ? __("NOT APPLICABLE") : $meta_value; // If empty show "NOT APPLICABLE"
        
        echo '<div class="type"><strong>' . $label . '</strong>: ' . $meta_value . '</div>';
    }
    echo '</div>';
    ?>
    

    I have changed a bit your html. If it’s not convenient and you want to keep yours, just replace:

        echo '<div class="type"><strong>' . $label . '</strong>: ' . $meta_value . '</div>';
    

    by this line:

        echo '<div class="type"><p>' . $label . '</p></div>' . $meta_value;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search