Last active
August 28, 2016 17:55
-
-
Save prasadnevase/06e67a053d214976a2214436177f3d53 to your computer and use it in GitHub Desktop.
WooCommerce Shortcode to get Ordered List of Attributes
This file contains hidden or 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 get_ordered_list_of_attributes(){ | |
global $product; | |
$formatted_attributes = array(); | |
$attributes = $product->get_attributes(); | |
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] = implode( ', ', wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) ) ); | |
} else { | |
$formatted_attributes[$attribute_label] = $attribute['value']; | |
} | |
} | |
} | |
foreach($formatted_attributes as $attr_key=>$attr_val){ | |
$attr_exploded = explode( ",", $attr_val ); | |
echo "<ol><li>" . $attr_key ; | |
echo "<ol>"; | |
for( $i=0; $i < sizeof($attr_exploded); $i++ ){ | |
echo "<li>" . $attr_exploded[$i] . "</li>"; | |
} | |
echo "</ol></li></ol>"; | |
} | |
} | |
add_shortcode( 'attributes_ordered_list', 'get_ordered_list_of_attributes' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment