Skip to content

Instantly share code, notes, and snippets.

@PechenkiUA
Last active September 28, 2023 13:47
Show Gist options
  • Save PechenkiUA/7b5e321dda15c585ce5658f6a73063a6 to your computer and use it in GitHub Desktop.
Save PechenkiUA/7b5e321dda15c585ce5658f6a73063a6 to your computer and use it in GitHub Desktop.
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