How to Get Current Product ID WooCommerce

WooCommerce Get Current Product ID
In this post, I want to share with you a WooCommerce get current product ID code snippet and a good example of how it works.

Getting the current product ID in WooCommerce is an important step when you are creating complex WooCommerce logic to customize or add features to your WooCommerce product.

To point out a specific example, you may want to get to the current WooCommerce product so that you can manipulate how it is added to the cart.

Another example is when you have a specific checkout functionality that needs the current product ID like the example I shared on how to create WooCommerce redirect after checkout per product.

So let us now look at how you can get your current product ID.

WooCommerce Get Current Product ID

WooCommerce Get Current Product ID -1

Suppose we want to get the ID of this current product that is shown in the image above when we visit this page.

The most important thing to understand when you are working with WooCommerce products as a WordPress developer – WooCommerce products are simply posts just like custom post types, pages, and posts.

With this in mind, you can simply understand that the best way to get the current product ID is to use the global post object as I will illustrate here with a quick example.

So we need the $post global object that returns all the details of a specific WordPress post.

post (WP_Post) The post object for the current post – Class WP_Post.

This is the WordPress core class that is used to implement the WP_Post object.

You can use the global $post variable to get access to the details of the current WordPress post.

So back to the example I highlighted in the image above, we can now use the global $post to get the current WooCommerce product ID:


global $post;

$current_product_ID = $post->ID;

WooCommerce Get Current Product ID Display WordPress Header

So we can go ahead and add this code to a header action hook and display the current WooCommerce product ID in the head as shown on the image below:WooCommerce Get Current Product ID

WooCommerce Get Current Product ID Step by Step (2 Steps)

Now let us look at what is happening here with a step-by-step explanation of the code snippet, which I will share below.

  1. First, we understand that a WooCommerce product is simply a post like another WordPress post.
  2. We can now use the global $post variable to obtain all the details of the current WooCommerce product.
  3. After we get the object with the details of the current WooCommerce product using the global $post variable we simply access the ID property.
  4. We have gone ahead and added an action hook to the header to just display the current product ID on the header of every post you visit. For example, we can open another WooCommerce and you will see the current product ID is displayed at the top as shown in the image below:WooCommerce Get Current Product ID -3

The following is the full code snippet that you can add to the functions.php file of your current theme to test it and see how it works!

add_action('wp_head', 'get_current_product_ID');
function get_current_product_ID(){
global $post;
$current_product_ID = $post->ID;
echo $current_product_ID;
}

Conclusion

In this post, I have shared with you a simple and straightforward way to get the current WooCommerce product ID.

The most important take from this tutorial is that WooCommerce products are simply posts and you can use the global $post WordPress variable to access the post object.

Similar Articles