gpt4 book ai didi

php - 获取要在 WooCommerce 3 中显示的产品价格

转载 作者:行者123 更新时间:2023-12-02 13:25:12 26 4
gpt4 key购买 nike

我想在我的主页显示 8 个类别 X 的产品

我使用以下代码来获取产品

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

有了这个我可以获得产品,但我不知道使用哪种方法来获得常规价格和促销价

最佳答案

Never use directly get_sale_price(); or get_regular_price(); WC_Product methods to display product prices.

Why? Because you will get the wrong prices in that 2 cases:

  • If you have enter your prices with taxes and you have set the display without taxes
  • If you have enter your prices without taxes and you have set the display with taxes.

因此,显示产品价格的正确方法是使用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() ) );

现在,如果您想要具有正确格式的货币价格,您还可以这样使用 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() ) ) );

关于php - 获取要在 WooCommerce 3 中显示的产品价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50653482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com