0

I am trying to show the price of a product in a post by code using HTML.

I am trying to utilize $product->get_price(). however, I need to be able to call the product by identifying it, using its code or something like it.

So Basically, all I want to do is to show the price of a particular product in the post by using a product ID as reference for example.

Any idea?

A J
  • 3,970
  • 14
  • 38
  • 53
  • 1
    Possible duplicate of [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) – dipak_pusti Sep 13 '18 at 05:57

2 Answers2

0

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

Hope this one helps.

dipak_pusti
  • 1,645
  • 2
  • 23
  • 42
  • Hi Dipak, Thank you for your reply. – Allenvik Sep 13 '18 at 11:00
  • Should the initial `$_product` information be placed on the functions and the get_price can be called on the HTML?
    Example:
    `$_product = wc_get_product( $product_id );`
    Should I specify the product or this function is generic? Should it be like
    '$_product = wc_get_product( $1234 );'`
    Regarding the '$_product->get_price();'
    Can it be called on HTML simply like that?
    `
    $_product->get_price();'
    `
    – Allenvik Sep 13 '18 at 11:17
  • 1st get the product object, then display the price. Here is the tutorial for newbies. [PHP Object](http://php.net/manual/en/language.types.object.php) – dipak_pusti Sep 14 '18 at 04:58
  • Hi Great Dipak, we are trying to understand it... let you know. Merry Christmas! – Allenvik Dec 20 '18 at 12:32
0

you use wc_get_product function :

$_product = wc_get_product( $product_id );
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Mehrshad Darzi
  • 101
  • 1
  • 4
  • Hi I really don't know how to make this think work... can an example be provided? TX – Allenvik Sep 13 '18 at 14:06
  • Do you mean using the Query Loop (WP_QUERY) for woocomerce? – Mehrshad Darzi Sep 13 '18 at 19:21
  • Hi, so what we wanted was to be able to bring the value of a woocommerce product. We learned that can be achieved by using a shortcode such as this one: [iconic_product_price id=”4937″]. However, we need to show the product value in a HTML table, and that is where we got stuck still. Any ideas are welcome. Thank you for the support so far. – Allenvik Dec 18 '18 at 18:44