0

I have the following code to display price once entering single product page.

 <?php 
   global $woocommerce;
   $product = new WC_Product(get_the_ID()); 
   echo $product->get_price_html(); //Shows the price
 ?>

I have tried serval variation of this code, but they all either error or empty value.

Although <?php echo get_the_title() ?> does work.

What can be causing get_price_html() to echo empty value?

1

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.