wordpress 如何在自定义页面上按 ID 号显示 Woocommerce 产品价格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30165014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:06:03  来源:igfitidea点击:

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

wordpresswoocommerceproduct

提问by MosesTheTool

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.

我正在尝试在自定义页面上显示 Woocommerce 中产品的价格。有一个简短的代码,但它给出了产品价格并添加了一个“添加到购物车按钮”,我不想要这个按钮,我只想通过 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>

回答by helgatheviking

If you have the product's ID you can use that to create a product object:

如果您有产品的 ID,则可以使用它来创建产品对象:

$_product = wc_get_product( $product_id );

Then from the object you can run any of WooCommerce's product methods.

然后,您可以从该对象运行任何 WooCommerce 的产品方法。

$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();

Update
Please review the Codex article on how to write your own shortcode.

更新
请查看有关如何编写自己的短代码的 Codex 文章。

Integrating the WooCommerce product data might look something like this:

集成 WooCommerce 产品数据可能如下所示:

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

你的简码看起来像 [woocommerce_price id="99"]

回答by Ash Patel

In woocommerce,

在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