71
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.
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 tutoraspire.com FREE
* @sourcecode https://tutoraspire.com/?p=17451
* @author Tutor Aspire
* @compatible WC 3.5.4
* @donate $9 https://tutoraspire.com
*/
add_action( 'woocommerce_after_shop_loop_item_title', 'tutoraspire_woocommerce_product_excerpt', 35 );
function tutoraspire_woocommerce_product_excerpt() {
global $post;
if ( is_home() || is_shop() || is_product_category() || is_product_tag() ) {
echo '';
echo get_post_meta( $post->ID, 'loopdesc', true );
echo '';
}
}
Here’s the final result: