This function displays custom meta values for a WooCommerce product in a table format using a WordPress shortcode. It only displays rows for non-empty values.
function display_custom_meta() {
    if (is_product()) {
        global $post;
        $product_id = $post->ID;
        $product = wc_get_product($product_id);
        // Fetching custom meta values
        $meta_keys = [
            'META_KEY_1_ROW_1', 'META_KEY_1_ROW_2',
            'META_KEY_2_ROW_1', 'META_KEY_2_ROW_2',
            'META_KEY_3_ROW_1', 'META_KEY_3_ROW_2',
            'META_KEY_4_ROW_1', 'META_KEY_4_ROW_2',
            'META_KEY_5_ROW_1', 'META_KEY_5_ROW_2',
            'META_KEY_6_ROW_1', 'META_KEY_6_ROW_2'
        ];
        $meta_values = array_map(function($key) use ($product) {
            return esc_html($product->get_meta($key));
        }, $meta_keys);
        // Constructing the table
        $output = '<table border="1" cellpadding="5" cellspacing="0">';
        $has_values = false;
        for ($i = 0; $i < count($meta_values); $i += 2) {
            if (!empty($meta_values[$i]) || !empty($meta_values[$i+1])) {
                $output .= '<tr><td>' . $meta_values[$i] . '</td><td>' . $meta_values[$i+1] . '</td></tr>';
                $has_values = true;
            }
        }
        $output .= '</table>';
        // Return the table if it contains values, otherwise return an empty string
        return $has_values ? $output : '';
    }
    return ''; // Return empty string if not a product page
}
add_shortcode('custom_meta', 'display_custom_meta');
Explanation
- Check if Product Page: The function checks if the current page is a WooCommerce product page using is_product().
- Retrieve Product and Meta Values: It retrieves the product ID and then fetches custom meta values using wc_get_productandget_meta.
- Store Meta Keys: Meta keys are stored in an array for easy management.
- Escape and Map Meta Values: The meta values are escaped and mapped into a new array.
- Construct Table: A table is constructed, adding rows only for non-empty meta values.
- Return Output: The table is returned if it contains values, otherwise, an empty string is returned.
- Add Shortcode: The function is hooked to a shortcode [custom_meta]for easy inclusion in posts or pages.