Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取要在WooCommerce 3中显示的产品价格_Php_Wordpress_Woocommerce_Product_Price - Fatal编程技术网

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

Php 获取要在WooCommerce 3中显示的产品价格,php,wordpress,woocommerce,product,price,Php,Wordpress,Woocommerce,Product,Price,我想在我的主页上显示8种X类产品 我使用以下代码来获取产品 <div class="row"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 8, 'product_cat' => 'cw'

我想在我的主页上显示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>


您可以使用,以销售价格获取销售价格,以常规价格获取常规价格

$product->get_sale_price();

$product->get_regular_price();
切勿直接使用
获取销售价格()
获取正常价格()
WC\u产品
显示产品价格的方法

为什么?因为在这两种情况下,您会得到错误的价格:

  • 如果您输入了含税的价格,并且设置了不含税的显示,
  • 如果您输入的价格不含税,并且设置了显示含税
所以显示产品价格的正确方法是使用
wc\u get\u price\u to\u 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() ) );
// 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() ) ) );
现在,如果您想使用货币获得正确的格式化价格,您还可以使用
wc\u price()
格式化功能,如下所示:

// 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() ) );
// 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() ) ) );

抱歉,实际批准的答案不正确,因为您希望显示价格…