Website Building » WooCommerce » How Do I Get WooCommerce Product Price in Loop?

How Do I Get WooCommerce Product Price in Loop?

Last updated on October 1, 2022 @ 11:49 pm

If you’re using WooCommerce to sell products on your WordPress site, you may find yourself wanting to display product prices in your site’s content. For example, you might want to list all of the products in a certain price range on a page.

Fortunately, there’s an easy way to do this.

You can use the get_price() method from the WooCommerce WC_Product class to get a product’s price.

Here’s a basic example of how to use the get_price() method:

$product = new WC_Product( $product_id );
 
$price = $product->get_price();
 
echo $price;

This will output the price of the product with an ID of $product_id. If you want to format the price as currency, you can use the wc_price() function:

$product = new WC_Product( $product_id );
 
$price = $product->get_price();
 
echo wc_price( $price );

How Do I Get WooCommerce Product Price in Loop?

PRO TIP: If you are using WooCommerce to sell products on your WordPress site, you may want to display the product price in the WordPress loop. However, this can be a tricky thing to do.

First off, you need to make sure that your WooCommerce products have pricing information entered. If you skip this step, WooCommerce will not be able to display prices for your products.

Next, you need to edit your WordPress theme files. Specifically, you need to edit the file called “single-product.php”. This file is responsible for displaying a single product on your site.

You will need to add some code to this file in order to display the product price in the WordPress loop. The code you need to add will vary depending on your theme, so it’s best to consult with a developer or someone who is familiar with PHP and WooCommerce.

Once you have added the necessary code, you should be able to see the product prices in the WordPress loop on your site.

By using the get_price() method from the WooCommerce WC_Product class, you can easily get a product’s price.

Dale Leydon

Dale Leydon

Sysadmin turned Javascript developer. Owner of 20+ apps graveyard, and a couple of successful ones.