WooCommerce Show Product Attribute On Archive Loop Items

WooCommerce product archive loop items showing attribute below the product name

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

This snippet allows you to get the WooCommerce product attributes and output them on the product archive loop items below the product name. In this particular example, we output the “color” product attribute. To output any other attribute, you’ll need to navigate to Dashboard > Products > Attributes > Create your attribute then configure your terms. You’ll see in the url a taxonomy parameter. Take the value from this parameter and replace pa_color in the code to output your attribute. You’ll see we’ve wrapped the output in the sale badge span class just to make it that little bit more presentable.

				
					/**
 * Snippet Name:     WooCommerce Show Product Attribute On Archive Loop Items
 * Snippet Author:   ecommercehints.com
*/

add_action( 'woocommerce_after_shop_loop_item_title', 'ecommercehints_show_product_attributes_on_arhive_loop_item', 1, 0 );
function ecommercehints_show_product_attributes_on_arhive_loop_item() {
	global $product;
	$product = wc_get_product( get_the_id() ); 
	$product_attributes = $product->get_attributes(); 
	$color_id = $product_attributes['pa_color']['options']['0']; // returns the ID of the term. You will need to change 'pa_color' to your taxonomy attribute ID.
	$color_name = get_term( $color_id )->name;
	echo '<span class="onsale">' . $color_name . '</span>';
};
				
			

Snippet Benefits

  • Show a product attribute on the product archive loop items, allowing users to see more product detail without having to drill-down into the product.
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

Your email address will not be published. Required fields are marked *

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers