A simple, powerful and independent e-commerce platform.
Sell anything with ease.

Create a shortcode to add a specific product price

You can easily add the Classic Commerce product price as a shortcode using the product ID as an attribute for any specific product.

The shortcode can be inserted into any page or post using the format: [cc_product_price id=321]

/**
 * Create a shortcode to add a specific product price
 */

// Creates a shortcode so that a product price can be inserted into any page or post.
// The shortcode takes the id as the parameter
// https://wpbeaches.com/add-woocommerce-product-price-as-a-shortcode/

add_shortcode('cl_product_price', 'cc_product_price_shortcode');

function cc_product_price_shortcode($atts) {
	
  $atts = shortcode_atts( array(
    'id' => null
  ), $atts, 'cl_product_price' );

  if (empty( $atts[ 'id' ])) {
    return '';
  }

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

  if (! $product) {
    return '';
  }
 
  return $product->get_price_html();
}
View Raw Code ID: 2620