Bah*_*ori 1 php wordpress product woocommerce price
我想在我的主页上显示8类产品
我使用以下代码来获取产品
<div class="row">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'product_cat' => 'cw'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
<div class="col-md-3">
<div class="product">
<?php echo woocommerce_get_product_thumbnail(); ?>
<p class="name"><?php echo get_the_title(); ?></p>
<p class="regular-price"></p>
<p class="sale-price"></p>
<a href="<?php echo get_permalink(); ?>" class="more">more info</a>
<form class="cart" action="<?php echo get_permalink(); ?>" method="post" enctype='multipart/form-data' style="display:inline;">
<button type="submit" name="add-to-cart" value="45" class="order">buy</button>
</form>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
有了这个,我可以得到产品,但我不知道使用哪种方法来获得定期和销售价格
切勿直接使用
get_sale_price();或使用get_regular_price();WC_Product方法显示产品价格.为什么?因为在这两种情况下你会得到错误的价格:
- 如果您输入了含税的价格,并且您已设置显示而不含税 ......
- 如果您输入了不含税的价格,并且您已将显示设置为税.
所以显示产品价格的正确方法是使用wc_get_price_to_display()这种方式:
// Active price:
wc_get_price_to_display( $product, array( 'price' => $product->get_price() ) );
//Regular price:
wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
//Sale price:
wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) );
Run Code Online (Sandbox Code Playgroud)
现在,如果您希望使用货币格式化正确的价格,您还将使用wc_price()格式化功能:
// Active formatted price:
$product->get_price_html();
// Regular formatted price:
wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) );
// Sale formatted price:
wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) );
Run Code Online (Sandbox Code Playgroud)
您可以使用 get_sale_price 获取促销价,使用 get_regular_price 获取正常价格
$product->get_sale_price();
$product->get_regular_price();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3025 次 |
| 最近记录: |