56

I'm trying to display a price of a product in Woocommerce, on a custom page. There is a short code for that, but it gives product price and also adds an "Add to cart button", I don't want the button, i just want to get the price of a specific product by ID.

Is this possible?

Thanks.

CODE:

<table class="unlockTableBorder">
<tbody>
<tr>
<td>
<h2>פתיחת מכשירי Alcatel כלל עולמי</h2>
<h4>אנא קראו והבינו את תנאי השירות הבאים לפני הזמנת שירות זה:</h4>
<ul>
 <li>שירות זה תומך בפתיחת מכשירים סלולריים מסוג Alcatel מארה"ב, קנדה ומקסיקו.</li>
 <li>מכשירי CDMA וספקיות שירות CDMA לא נתמכים בידי שירות זה, אנא אל תשתמשו בשירות זה בשביל מכשירים אלו - במידה ותשמשו בשירות זה למכשירי CDMA, אתם תקבלו קוד שלא תוכלו להשתמש בו, ולא תוכלו לקבל החזר כספי! - אנא <a title="פתיחת מכשירי CDMA לכל הרשתות" href="http://www.unlocker.co.il/sim-unlock-cdma-mobile-device/">ראו פתיחת מכשירי CDMA לכל הרשתות בישראל.</a></li>
</ul>
<h5><strong>זמן הספקה: 1-24 שעות</strong></h5>
<form id="unlock1" class="cart" enctype="multipart/form-data" method="post" name="unlock"><input class="the_imei" style="width: 80%; border-radius: 15px;" name="the_imei" type="text" value="" placeholder="מספר סידורי IMEI של המכשיר (חייג #06#*)" /> <input class="add-to-cart" name="add-to-cart" type="hidden" value="76" /> <button class="unlockButton" type="submit" value="submit">פתח לכל הרשתות בישראל </button></form>*בלחיצה על הפתור, אתם מסכימים ל<a title="תנאי השירות" href="http://www.unlocker.co.il/terms-and-conditions/">תנאי השירות</a>.</td>
</tr>
</tbody>
</table>
<script src="http://www.unlocker.co.il/checkimei1.js" type="text/javascript"></script>
MosesTheTool
  • 695
  • 1
  • 5
  • 7
  • Please provide the code that you tried – Gunaseelan May 11 '15 at 10:15
  • You mean the shortcode? – MosesTheTool May 11 '15 at 10:27
  • This is not the full shortcode. It appears to only be a form. How are you getting the product ID? And where should the product price be displayed? – helgatheviking May 11 '15 at 17:18
  • Actually, I don't know where the Shortcode is located, I wasn't talking about my own shortcode, I don't know how to create one. - I was asking for a way to display a product Price ONLY on a custom wordpress page, maybe by Shortcode, because it seems to work best, and the only "official" shortcode by wordpress displays the ADD TO CART button along with the price, I do not want the ADD TO CART button – MosesTheTool May 12 '15 at 06:45
  • Placing Helga's code in the functions.php file worked perfectly. – MCHart Oct 05 '15 at 17:46

3 Answers3

115

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"]

helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • I appreciate the help, but with both answers, I have no idea how to INSERT this to the HTML of my Custom Pages, Can you be more specific on what I need to do with this to display a price of a product by ID on a custom page? thanks – MosesTheTool May 11 '15 at 20:21
  • No, not really. This answers what you asked about getting the price of a product. We can't do more until you provide more detail. See my comment to your question for a start. – helgatheviking May 11 '15 at 22:12
  • I am unsure what needs to be done to get the final result of the price in a custom wordpress page by some sort of code\shortcode, i just need to have a way of inserting the price by product ID as plain text. – MosesTheTool May 12 '15 at 02:58
  • I found a plugin called "Shortcoder" which allows me to create shortcodes, any way I can use the lines of code you gave me to create a short code with it? – MosesTheTool May 12 '15 at 10:24
  • I have no experience with this plugin. I've updated my answer with how to write a sample shortcode. Please review the linked Codex article. – helgatheviking May 12 '15 at 12:36
  • For older woocommerce version, replace 'wc_get_product' with 'get_product' – azeem Oct 14 '15 at 04:01
  • 4
    I always encourage people not to use older versions of WooCommerce. – helgatheviking Oct 14 '15 at 14:53
30

In woocommerce,

Get regular price :

$price = get_post_meta( get_the_ID(), '_regular_price', true);
// $price will return regular price

Get sale price:

$sale = get_post_meta( get_the_ID(), '_sale_price', true);
// $sale will return sale price
Davey
  • 2,355
  • 1
  • 17
  • 18
Ashish Patel
  • 3,551
  • 1
  • 15
  • 31
  • Thank you, but how do i use it to put on the custom page? my programming knowledge is very limited – MosesTheTool May 11 '15 at 10:20
  • @AshPatel If third parameter is set to true, it means it will return `value` and woocommerce stores it as string not as an array. – Rohil_PHPBeginner May 11 '15 at 10:26
  • I pasted the custom page code, but i have many other pages, i need to know how to make it work on other pages as well. just to understand how to add the price to custom pages by ID – MosesTheTool May 11 '15 at 10:28
  • @rohil....true...but get_post_meta will return value of regular and sale price stored in DB..it wont return value from woo commerce..it return value from post meta table by post id and meta key directly... – Ashish Patel May 11 '15 at 10:33
  • [codex](https://codex.wordpress.org/Function_Reference/get_post_meta#Parameters) :) – Rohil_PHPBeginner May 11 '15 at 10:34
  • i'm not sure how to add this to the site and then get the price with some HTML code on the custom page.... help? – MosesTheTool May 11 '15 at 10:39
  • @MosesTheTool you need to modify function which execute short code – Ashish Patel May 11 '15 at 11:06
  • @AshPatel where do i find it? – MosesTheTool May 12 '15 at 06:47
  • @AshPatel ok I found where i can edit the shortcodes, but I have little understanding in PHP programming, so I tried copying the Shortcode that gives the price+add-to-cart-button, and deleting the line that gives the button, but all of my site errored, so i needed to return it to the original state. I installed a plugin called Shortcoder, which allows you to create shortcodes, but it only works with HTML so i am not sure how to add the PHP inside it – MosesTheTool May 12 '15 at 11:25
  • @Ash Patel: how I will get add cart link using above query. – Harshad Patil Jan 16 '17 at 05:54
  • I think it won't take hooks into account which were applied. It will directly get price from database and neglect all the price hooks. Proper way is to price by using woocommerce functions for price. – Hassan Dad Khan Aug 15 '17 at 07:49
9

Other answers work, but

To get the full/default price:

$product->get_price_html();

Adrian Enriquez
  • 8,175
  • 7
  • 45
  • 64