Last active
September 18, 2018 10:49
-
-
Save iWebbers/bf7b4fde053fbf9011f3acb7b574dba8 to your computer and use it in GitHub Desktop.
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
// 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