WooCommerce wc_get_product Function And How To Use It

wc_get_product Function

If you’re a WooCommerce developer building an e-commerce site for a client, you may find yourself in a situation where you need to get a WooCommerce product (WC_Product) object or transform the current $post into a WC_Product object.

Fortunately, WooCommerce has a handy function wc_get_product which we’re going to take a closer look at today.

wc_get_product Function And Its’ Arguments

The wc_get_product($the_product = null, array $deprecated = []) function has 2 arguments:

  • $the_product – Post object or post ID of the product
  • $deprecated – Previously used to pass arguments to the factory, e.g. to force a type. This argument is no longer used

The function will return either an instance of a WC_Product object or null. If you call this function too early, before woocommerce_init action is finished, the function will return false.

How To Use wc_get_product Function

The function is pretty straightforward to use. Here are the 2 examples: getting a product by id, and getting the product from the current post (this example will only work if you’re on the single product page):


$product_id = 11;
$product = wc_get_product($product_id);

global $post;

$product = wc_get_product($post);
// OR
$product = wc_get_product($post->ID);

Functions Similar To wc_get_product

There are some other functions in WooCommerce that are similar to the wc_get_product function:

  • wc_get_products(array $args) – allows you to get a list of products based on provided parameters in the $args argument
  • wc_get_product_object($product_type, $product_id = 0) – gets the type of the product object

Wrapping Up

As you can see, the wc_get_product is a simple function that can be quite handy and flexible. Hopefully, you can start using it in your next WooCommerce project.

About The Author

Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

The Ultimate Managed Hosting Platform

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.