Making Product Price in WooCommerce as a Shortcode

Looking to insert a WooCommerce product price into a layout, well you can add the WooCommerce Product Price as a shortcode using the product ID as an attribute for the correct product.

The code below goes into your functions.php file, the add the shortcode where you need to display the price.

add_shortcode( 'codeithub_price', 'codeithub_woo_product_price_shortcode' );
/**
 * Shortcode WooCommerce Product Price.
 *
 */
function codeithub_woo_product_price_shortcode( $atts ) {
	
	$atts = shortcode_atts( array(
		'id' => null
	), $atts, 'codeithub_price' );
 
	if ( empty( $atts[ 'id' ] ) ) {
		return '';
	}
 
	$product = wc_get_product( $atts['id'] );
 
	if ( ! $product ) {
		return '';
	}
 
	return $product->get_price_html();
}
[codeithub_price id="1011"]

Leave a Reply

Your email address will not be published. Required fields are marked *