Created
May 2, 2018 01:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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