How can i display product attribute name in WooCommerce cart page

If we want to show product attribute name for variable or simple product on cart page, we 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 attribute name after the title of the product using a action hook called woocommerce_after_cart_item_name.

Leave a Reply

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