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

Mos*_*ool 43 wordpress product woocommerce

我试图在自定义页面上显示Woocommerce中的产品价格.有一个简短的代码,但它提供了产品价格,并添加了"添加到购物车按钮",我不想要按钮,我只是想通过ID获得特定产品的价格.

这可能吗?

谢谢.

码:

<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>
Run Code Online (Sandbox Code Playgroud)

hel*_*ing 91

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

$_product = wc_get_product( $product_id );
Run Code Online (Sandbox Code Playgroud)

然后从对象中运行任何WooCommerce的产品方法.

$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Run Code Online (Sandbox Code Playgroud)

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

整合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' );
Run Code Online (Sandbox Code Playgroud)

你的短代码就像是 [woocommerce_price id="99"]

  • 我总是鼓励人们不要使用旧版本的WooCommerce. (4认同)

Ash*_*tel 23

在woocommerce中,

定价:

$price = get_post_meta( get_the_ID(), '_regular_price', true);
// $price will return regular price
Run Code Online (Sandbox Code Playgroud)

获得促销价:

$sale = get_post_meta( get_the_ID(), '_sale_price', true);
// $sale will return sale price
Run Code Online (Sandbox Code Playgroud)


Adr*_*uez 9

其他答案有效,但是

要获取完整/默认价格:

$product->get_price_html();

  • 以 2021 年的方式,这应该比现在所有其他答案更受青睐。 (2认同)