Menu Close

How to display WooCommerce product price using a shortcode?

codeamend

Let’s see how to display WooCommerce product price using a shortcode.

Refer the below snippet to get specific product price details…

add_shortcode( 'productprice', 'cwa_productprice_func' );

function cwa_productprice_func( $atts ) {
    $product = wc_get_product( $atts['id'] );
    return '<div class="woocommerce"><div class="product"><p class="'.esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ).'">'.$product->get_price_html().'</p></div></div>';
} 

Update this php code in function.php and use shorcode [productprice id=”product ID”] to get custom product price.

Share

Posted in WooCommerce, WordPress, WordPress Shortcodes

You can also read...