Skip to content

Instantly share code, notes, and snippets.

@iWebbers
Last active September 18, 2018 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iWebbers/bf7b4fde053fbf9011f3acb7b574dba8 to your computer and use it in GitHub Desktop.
Save iWebbers/bf7b4fde053fbf9011f3acb7b574dba8 to your computer and use it in GitHub Desktop.
// Get WooCommerce product attributes by ID via shortcode: [iw_product_attributes id='']
function iw_product_attributes_shortcode( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$_product = wc_get_product( $atts['id'] );
if( $_product->has_attributes() ){
// Initializing
$attributes = array();
// Loop through product attributes
foreach( $_product->get_attributes() as $taxonomy => $attribute ){
// The product attribute label name
$attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
// Set each product attribute with its values in an array
$attributes[] = '<li><span class="uk-text-uppercase uk-text-lead">' . $attribute_name . '</span><br />' . $_product->get_attribute($taxonomy) . '</li>';
}
// Display (output)
return '<ul class="uk-list uk-list-divider">'. implode( $attributes ) . '</ul>';
}
}
}
add_shortcode( 'iw_product_attributes', 'iw_product_attributes_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment