I am new to WooCommerce and am looking for a solution to display Product attributes on a post page. I've made research and some test but nothing seems to work.

Ideally, I would like to use a shortcode taking the Product ID and displaying all his attributes on my post page. something like [product_page id="99" display_only_attributes ]

1 Answers

0
LoicTheAztec On Best Solutions

Here is the way to get the product attributes from a custom shortcode where you will define the product ID in as a shortcode argument id.

The function code:

add_shortcode("product_attributes", "display_product_attributes");
function display_product_attributes( $atts ) {

    // Shortcode attribute (or argument)
    $atts = shortcode_atts(
        array(
            'id'    => ''
        ),
        $atts, 'product_attributes'
    );

    if( ! ( isset($atts['id']) && $atts['id'] > 0 ) ) return;

    $product = wc_get_product($atts['id']);

    ob_start();

    do_action( 'woocommerce_product_additional_information', $product );

    return ob_get_clean();
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

SHORTCODE USAGE (whith a defined product id):

[product_attributes id='37']

OR in php:

echo do_shortcode("[product_attributes id='37']");

You will get something like this:

enter image description here