Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wooccomerce list attribute by product
<?php
function attributes(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach($attributes as $attr=>$attr_deets){
$attribute_label = wc_attribute_label($attr);
if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {
$attribute = isset( $attributes[ $attr ] ) ? $attributes[ $attr ] : $attributes[ 'pa_' . $attr ];
if ( $attribute['is_taxonomy'] ) {
$formatted_attributes[$attribute_label]['name'] = implode( ', ', wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) ) );
$formatted_attributes[$attribute_label]['id'] = $attr_deets['id'];
} else {
$formatted_attributes[$attribute_label]['name'] = $attribute['value'];
}
}
}
return $formatted_attributes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment