如何在自定义页面上按 ID 号显示 Woocommerce 产品价格?
How to display Woocommerce product price by ID number on a custom page?
我正在尝试在自定义页面上显示 Woocommerce 中的产品价格。
有一个简短的代码,但它给出了产品价格,还添加了一个 "Add to cart button",我不想要按钮,我只想通过 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>
在 woocommerce 中,
获取正常价格:
$price = get_post_meta( get_the_ID(), '_regular_price', true);
// $price will return regular price
获取促销价:
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
// $sale will return sale price
如果您有产品 ID,则可以使用它来创建产品对象:
$_product = wc_get_product( $product_id );
然后您可以从该对象 运行 任何 WooCommerce 的产品方法。
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
更新
请查看有关如何 write your own shortcode 的 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' );
你的短代码看起来像 [woocommerce_price id="99"]
其他答案有效,但是
获取full/default价格:
$product->get_price_html();
我正在尝试在自定义页面上显示 Woocommerce 中的产品价格。 有一个简短的代码,但它给出了产品价格,还添加了一个 "Add to cart button",我不想要按钮,我只想通过 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>
在 woocommerce 中,
获取正常价格:
$price = get_post_meta( get_the_ID(), '_regular_price', true);
// $price will return regular price
获取促销价:
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
// $sale will return sale price
如果您有产品 ID,则可以使用它来创建产品对象:
$_product = wc_get_product( $product_id );
然后您可以从该对象 运行 任何 WooCommerce 的产品方法。
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
更新
请查看有关如何 write your own shortcode 的 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' );
你的短代码看起来像 [woocommerce_price id="99"]
其他答案有效,但是
获取full/default价格:
$product->get_price_html();