Skip to content

Instantly share code, notes, and snippets.

@carlosvarela
Created May 2, 2018 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosvarela/6e2a498abab8364489307494d2791797 to your computer and use it in GitHub Desktop.
Save carlosvarela/6e2a498abab8364489307494d2791797 to your computer and use it in GitHub Desktop.
Displays product attributes in the top right of the single product page in Woocommerce
/**
* Displays product attributes in the top right of the single product page.
*
* @param $product
*/
function display_attributes( $product ) {
global $product;
global $post;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) {
// Get the taxonomy.
$terms = wp_get_post_terms( $product->id, $attribute[ 'name' ], 'all' );
$taxonomy = $terms[ 0 ]->taxonomy;
// Get the taxonomy object.
$taxonomy_object = get_taxonomy( $taxonomy );
// Get the attribute label.
$attribute_label = $taxonomy_object->labels->name;
// Display the label followed by a clickable list of terms.
echo get_the_term_list( $post->ID, $attribute[ 'name' ] , '<div class="attributes">' . $attribute_label . ': ' , ', ', '</div>' );
}
}
add_action( 'woocommerce_product_meta_end', 'display_attributes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment