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

How Do I Get the WooCommerce Product Price in WordPress?

Last updated on October 1, 2022 @ 5:30 am

If you’re running a WooCommerce store on your WordPress site, you might need to get the product price for various reasons. For example, you might want to display the price of a product in a custom WooCommerce widget or on a custom WooCommerce page template.

Getting the product price in WooCommerce is actually pretty simple. In this article, we’ll show you how to get the product price in WordPress, and we’ll also provide some useful code snippets that you can use to get the price of a product in various scenarios.

First, let’s take a look at how to get the product price in WordPress using the get_post_meta() function:

$product_id = 123;
$product_price = get_post_meta( $product_id, ‘_price’, true );
echo $product_price;

This code snippet will retrieve the price of the product with an ID of 123 and then echo it out to the screen. You can use this same code snippet to get the price of any other product on your WooCommerce site.

Now let’s take a look at how to get the product price in WordPress using the WC_Product::get_price() method:

$product = wc_get_product( $product_id );
$product_price = $product->get_price();
echo $product_price;

This code snippet does the same thing as the first one. It retrieves the price of the product with an ID of 123 and then echoes it out to the screen. However, this code snippet uses the WC_Product::get_price() method, which is part of the WooCommerceProducts class.

Here are some other useful methods that you can use to get various pieces of information about a product:

// Get the sale price of a product (if there is one)
$sale_price = $product->get_sale_price();

// Get the regular price of a product
$regular_price = $product->get_regular_price();

// Get the total amount (sale price + regular price)
$total_amount = $product->get_price();

Conclusion:

As you can see, getting the product price in WordPress is actually pretty simple. There are two main ways to do it, and both are pretty straightforward. If you need to get more information about a product than just its price, then you can use one of the other methods that we mentioned above.

End of Article

.

Morgan Bash

Morgan Bash

Technology enthusiast and Co-Founder of Women Coders SF.