Custom fields woocommerce

I am displaying some custom fields on the woocommerce single product page with this

add_action( 'woocommerce_single_product_summary','add_custom_field', 20 );
function add_custom_field() {
   global $post;
   echo get_post_meta( $post->ID, 'Brand', true );
   echo get_post_meta( $post->ID, 'Content', true );
   return true;
}

This dispays only the values of the custom fields, but I would like the names before so it would look like this:

Read More

Brand: …
Content: …

The custom fields don’t aplly to every product though, so for the products where the custom fields are not set, nothing should be displayed.

Related posts

Leave a Reply

2 comments

  1. Use this:

    add_action( 'woocommerce_single_product_summary', 'add_custom_field', 20 );
    function add_custom_field() {
        global $post;
        $brand =   get_post_meta( $post->ID, 'Brand', true );
        $content = get_post_meta( $post->ID, 'Content', true );
        if (!empty($brand)) {
            echo 'Brand: '. $brand;
        }
        if (!empty($content)) {
            echo 'Content: '. $content;
        }
    }
    
  2. Try below code

    add_action( 'woocommerce_single_product_summary', 'add_custom_field', 20 );
    function add_custom_field() {
        global $post;
        $brand =   get_post_meta( $post->ID, 'Brand', true );
        $content = get_post_meta( $post->ID, 'Content', true );
        if (!empty($brand)) {
            echo 'Brand: '. $brand .'<br>';
        }
        if (!empty($content)) {
            echo 'Content: '. $content .'<br>';
        }
    }