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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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();
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0