How to display Woocommerce product price by ID number on a custom page?
Asked Answered
C

3

58

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>
Corotto answered 11/5, 2015 at 10:14 Comment(5)
Please provide the code that you triedRackety
You mean the shortcode?Corotto
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?Ahl
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 buttonCorotto
Placing Helga's code in the functions.php file worked perfectly.Prognostic
A
120

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

Ahl answered 11/5, 2015 at 17:17 Comment(7)
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? thanksCorotto
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.Ahl
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.Corotto
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?Corotto
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.Ahl
For older woocommerce version, replace 'wc_get_product' with 'get_product'Mcalister
I always encourage people not to use older versions of WooCommerce.Ahl
P
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
Proximate answered 11/5, 2015 at 10:19 Comment(11)
Thank you, but how do i use it to put on the custom page? my programming knowledge is very limitedCorotto
@AshPatel If third parameter is set to true, it means it will return value and woocommerce stores it as string not as an array.Jairia
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 IDCorotto
@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...Proximate
codex :)Jairia
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?Corotto
@Corotto you need to modify function which execute short codeProximate
@AshPatel where do i find it?Corotto
@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 itCorotto
@Ash Patel: how I will get add cart link using above query.Sofko
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.Pitchman
M
10

Other answers work, but

To get the full/default price:

$product->get_price_html();

Millicentmillie answered 24/2, 2021 at 11:47 Comment(1)
the 2021 way, this should be preferred over all the other answers now.Ranit

© 2022 - 2024 — McMap. All rights reserved.