Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hemusyl/c9365f7c241718f4c4a57fa198db5bee to your computer and use it in GitHub Desktop.
Save hemusyl/c9365f7c241718f4c4a57fa198db5bee to your computer and use it in GitHub Desktop.
How to display Woocommerce product price by ID number on a custom page?
https://stackoverflow.com/questions/30165014/how-to-display-woocommerce-product-price-by-id-number-on-a-custom-page
If you have the product's ID you can use that to create a product object:
$_product = wc_get_product( $product_id );
Then from the object you can run any of WooCommerce's product methods.
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Update
Please review the Codex article on how to write your own shortcode.
Integrating the WooCommerce product data might look something like this:
function so_30165014_price_shortcode_callback( $atts ) {
$atts = shortcode_atts( array(
'id' => null,
), $atts, 'bartag' );
$html = '';
if( intval( $atts['id'] ) > 0 && function_exists( 'wc_get_product' ) ){
$_product = wc_get_product( $atts['id'] );
$html = "price = " . $_product->get_price();
}
return $html;
}
add_shortcode( 'woocommerce_price', 'so_30165014_price_shortcode_callback' );
Your shortcode would then look like [woocommerce_price id="99"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment