Skip to content

Instantly share code, notes, and snippets.

@eljamez
Created August 28, 2013 17:49
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save eljamez/6369031 to your computer and use it in GitHub Desktop.
Save eljamez/6369031 to your computer and use it in GitHub Desktop.
Woocommerce, get the product's attribute title from attribute slug
// just pass in the attribute and the attribute slug
// and the return value is the attribute's name
// example : (assuming attribute size has the option of "Extra Small" withe the slug of "extra-small")
// echo attribute_slug_to_title('attribute_pa_size', 'extra-small');
// returns
// "Extra Small"
// code reworked from woocommerce/classes/class-wc-cart.php
// attribute slug to title
if ( ! function_exists( 'attribute_slug_to_title' ) ) {
function attribute_slug_to_title( $attribute ,$slug ) {
global $woocommerce;
if ( taxonomy_exists( esc_attr( str_replace( 'attribute_', '', $attribute ) ) ) ) {
$term = get_term_by( 'slug', $slug, esc_attr( str_replace( 'attribute_', '', $attribute ) ) );
if ( ! is_wp_error( $term ) && $term->name )
$value = $term->name;
} else {
$value = apply_filters( 'woocommerce_variation_option_name', $value );
}
return $value;
}
}
@jesismaelv
Copy link

love it, great, it just works!

@jensquast
Copy link

Great! Thank you very much!

@dergoldbroiler
Copy link

merci beaucoup :-)

@ShinekhuuD
Copy link

ShinekhuuD commented May 30, 2019

Thank you, I used your code, your boy is still killing it and works perfectly

@simplenotezy
Copy link

$value = apply_filters( 'woocommerce_variation_option_name', $value ); $value will be undefined.

@njbair
Copy link

njbair commented Nov 23, 2020

$value = apply_filters( 'woocommerce_variation_option_name', $value ); $value will be undefined.

You are correct. Instead you should use $slug:

$value = apply_filters( 'woocommerce_variation_option_name', $slug );

@DeniscoDp5
Copy link

nice :D

@andrewinsidelazarev
Copy link

How to get ID from a attribute's slug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment