Try the following shortcode based on single-product/product-attributes.php
Woocommerce template, that will display product weight, dimensions and product attributes. You can specify the product ID using the id
attribute:
[product_taxonomies id="37"]
or used in php code:
echo do_shortcode("[product_taxonomies id='37']");
or without specifying the product ID (takes the current post ID):
[product_taxonomies]
The code:
add_shortcode( 'product_taxonomies', 'product_taxonomies_shortcode' );
function product_taxonomies_shortcode( $atts ){
// Shortcode Attributes
$atts = shortcode_atts( array(
'id' => '',
), $atts, 'product_taxonomies' );
$product_id = ! empty($atts['id']) ? $atts['id'] : 0;
if( $product_id > 0 ) {
$product = wc_get_product( $product_id );
} else {
global $product;
}
if( ! is_a($product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_id() );
}
if ( is_a($product, 'WC_Product' ) ) {
ob_start();
// Weight
if ( $product->has_weight() ) {
$weight_unit = get_option('woocommerce_weight_unit');
$weight_html = '<strong>'.__("Weight").':</strong> ' . $value . $weight_unit;
echo '<div class="dimensions">' . $weight_html . '</div>';
}
// Dimensions
if ( $product->has_dimensions() ) {
$dimension_unit = get_option('woocommerce_dimension_unit');
$dimensions = array();
foreach( $product->get_dimensions( false ) as $key => $value ) {
if( ! empty($value) )
$dimensions[] = '<strong>'.ucfirst($key).':</strong> ' . $value . $dimension_unit;
}
echo '<div class="dimensions">' . implode('<br>', $dimensions) . '</div>';
}
// Product attributes (visible)
if ( $attributes = array_filter( $product->get_attributes(), 'wc_attributes_array_filter_visible' ) ) {
foreach ( $attributes as $attribute ) {
$attribute_label_name = '<strong>'.wc_attribute_label( $attribute->get_name() ).':</strong> ';
$values = array();
if ( $attribute->is_taxonomy() ) {
$attribute_taxonomy = $attribute->get_taxonomy_object();
$attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
foreach ( $attribute_values as $attribute_value ) {
$value_name = esc_html( $attribute_value->name );
if ( $attribute_taxonomy->attribute_public ) {
$values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
} else {
$values[] = $value_name;
}
}
} else {
$values = $attribute->get_options();
foreach ( $values as &$value ) {
$value = make_clickable( esc_html( $value ) );
}
}
echo '<div class="'.$attribute->get_name().'">' . $attribute_label_name . implode( ', ', $values ) . '</div>';
}
}
return ob_get_clean();
}
}
This code goes in function.php file of your active child theme (or active theme). Tested and works