A client asked me to show a given custom field in the loop (i.e. Shop page, Category pages, Tag pages = anywhere woocommerce products are returned). Interestingly enough, she didn’t want to show the product short description (see “show product short description on the homepage only” snippet) but a custom field, so here’s how you do it!

1. Add a Custom Field to Each Single Product

Go to the single product page, scroll down to the custom fields box, and then add a new one manually. Call it something like “loopdesc” and add the text you want to show in the product category pages.

WooCommerce Product Add New Custom Field
WooCommerce Product Add New Custom Field WooCommerce Product Add New Custom Field

2. Add PHP to your functions.php to Show Custom Field @ Loop


/**
 * @snippet       WooCommerce Show Product Custom Field in the Category Pages
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=17451
 * @author        Rodolfo Melogli
 * @compatible    WC 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_after_shop_loop_item_title', 'bbloomer_woocommerce_product_excerpt', 35 );  

function bbloomer_woocommerce_product_excerpt() {
global $post;
if ( is_home() || is_shop() || is_product_category() || is_product_tag() ) {
   echo '<span class="excerpt">';
   echo get_post_meta( $post->ID, 'loopdesc', true );
   echo '</span>';
}
}

Here’s the final result:

WooCommerce Show Custom Field in the Loop
WooCommerce Show Custom Field in the Loop WooCommerce Show Custom Field in the Loop
Share:

Leave a Reply