Menu Close

WooCommerce: Products Attributes in a Shortcode

default

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!

I am now able to display the attributes table for product ID = 214015 on a custom page, thanks to the snippet below.

PHP Snippet: Display Product Attribute Table Anywhere Via a Shortcode

/**
 * @snippet       Product Attributes Shortcode
 * @usage         [prod_atts pid="12345"]
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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();
}
View Source
Posted in WooCommerce Tips