Products attributes display on the single product page whenever attribute terms are assigned to a given WooCommerce product. This is the default behaviour.
However, what if you want to show the product attribute table somewhere else? For example, in a blog post, or in a custom pricing table?
Well, we can build a shortcode for that – simply specify a product ID and the shortcode will magically output its attributes. Enjoy!
PHP Snippet: Display Product Attribute Table Anywhere Via a Shortcode
/**
* @snippet Product Attributes Shortcode
* @usage [prod_atts pid="12345"]
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 7
* @community https://businessbloomer.com/club/
*/
add_shortcode( 'prod_atts', 'bbloomer_show_product_atts' );
function bbloomer_show_product_atts( $atts ) {
if ( ! $atts['pid'] ) return;
$product = wc_get_product( $atts['pid'] );
if ( ! is_a( $product, 'WC_Product' ) ) {
return;
}
ob_start();
do_action( 'woocommerce_product_additional_information', $product );
return ob_get_clean();
}
Hello,
Can i use this shortcut in SMS for WooCommerce plugin?
Not sure, never tried it before. You’ll need to test it
hello,
thanks for the above, how can I add nested attributes to the product
What do you mean by “nested attributes”?
Hello,
Thanks for the above, how about just getting the attribute value in the shortcode?
Thanks
Mark
Mark, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Is it possible to add the product ID automatically?
As long as you’re on the single product page, you could try with:
Hi Rodolfo,
Could this also made to work for a specific attribute? I want to show only two attributes in a specific place in the product page. This should not be so hard but I can’t find it. I would think that your code with a bit development should be able to show this. But I am not a coder so maybe not 🙂
Cheers
Marco
Hey Marco, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!