php - WooCommerce how to get a clean label of a product attributes on front end

one text

Solution:

"on the frontend the attributes appear as the raw name (e.g. pa_variation_name) rather than a clean looking title (e.g. Variation Name)."

I would use woocommerce wc_attribute_label function. Like this:

wc_attribute_label( $attribute->->get_name() );

For example if I run it on one of my products:

$attributes = $product->get_variation_attributes();

foreach ($attributes as $attribute_name => $attribute_options) 
{
  echo wc_attribute_label($attribute_name);
}

It'll output this:

enter image description here

Note:

  • It's been capitalized.
  • pa_ has been removed.

But if I run my code without wc_attribute_label:

$attributes = $product->get_variation_attributes();

foreach ($attributes as $attribute_name => $attribute_options) 
{
  echo $attribute_name;
}

It'll output this:

enter image description here


For more details you could checkout wc_attribute_label function.

Source