Last active
September 28, 2023 13:47
-
-
Save PechenkiUA/7b5e321dda15c585ce5658f6a73063a6 to your computer and use it in GitHub Desktop.
Wooccomerce list attribute by product
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
<?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