How can i display product attribute name in WooCommerce cart page

If we want to show the product attribute name for a variable or simple product on the cart page, you need to add the below code on your theme functions.php.

add_action('woocommerce_after_cart_item_name', 'wa_woocommerce_after_cart_item_name',10,2);

function wa_woocommerce_after_cart_item_name( $cart_item, $cart_item_key ){
	$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
	$attributes = $_product->get_attributes();

	if ( !empty( $attributes ) ) {
		$attr_arr = [];
		foreach( $attributes as $attr => $attr_val ) {

			if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {

				$term_name = $_product->get_attribute( $attr );

				if ( !empty( $term_name ) ) {
					$attr_arr[] = $term_name;
				}

			}
		}
	}

	if ( !empty( $attr_arr )) {
		echo implode( ',', $attr_arr );
	}
	
}

In my case, I added an attribute name after the product’s title using an action hook called woocommerce_after_cart_item_name.

Leave a Reply

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