Woocommerce, get current product id

I'm currently working on a WooCommerce theme and attempting to add a sidebar to the product detail page.

I've been able to get the sidebar added (specifically, this one: http://woocommerce.wp-a2z.org/oik_file/templatescontent-widget-product-php/)

Now, I'm trying to figure out how to add a class of "active" to the currently selected product and can't seem to figure it out?

In other words, how do I do something along the lines of if the current product id is equal to the product id in the sidebar add class="active" to the li?

I've made numerous searches and haven't been able to come up with anything useful, so I'm turning here.

Any help greatly appreciated!!

Solutions

If the query hasn't been modified by a plugin for some reason, you should be able to get a single product page's "id" via

global $post;
$id = $post->ID

OR

global $product;
$id = $product->id;

EDIT: As of WooCommerce 3.0 this needs to be

global $product;
$id = $product->get_id();

2017 Update - since WooCommerce 3:

global $product;
$id = $product->get_id();

Woocommerce doesn't like you accessing those variables directly. This will get rid of any warnings from woocommerce if your wp_debug is true.

Since WooCommerce 2.2 you are able to simply use the wc_get_product Method. As an argument you can pass the ID or simply leave it empty if you're already in the loop.

wc_get_product()->get_id();

OR with 2 lines

$product = wc_get_product();
$id = $product->get_id();

Similar questions

Total number of order as per current month and product added by logged user on product list page using woocommerce
I want to display total number of order in product list page. I can get simple count but not get with below conditions. 1) Display on product list page 2) Product added by logged in user not all user 3) Order of current month (not product) I have tried below code.I have added this hook to my theme function file. Below id the place where i want to d...
how to change woocommerce price style but only on single product page for the current product
Hi I need to change the product price of the selected product on Woocommerce i have this function the problem is that function executes on the product page... but also on the related products on the bottom of the page. how can I identify only the CURRENT PRODUCT PRICE ?! Thanks !
Woocommerce : Add Body class to product page if the current product is in the cart
I found this great snippet from here https://gist.github.com/michaelbourne/a2fbebbc839a41c4204524fcd020061e The following is the function to add a body class to the Woocomerce product page if the cart contains any product. Does anyone know how to modify the above code snippet to add a class to the body of the product page when the cart contains the...
How can I add below of Woocommerce out of stock product text the Woocommerce product price on a single product
I have a single woocommerce product page where only availability "Out of Stock" message is shown. How can I show a formatted price with currency below that text? I think I need to insert the following but I dont know how This is happening to all simple products. As settings, stock management is disabled. I don't know why price is not displayed. I t...
Get titles of all posts with current tags except current post
I am trying to print out a bulleted list of all posts in the current posts category (i.e. "siblings") except for the current post itself. I have been able to get the tags of the current bost and print out the list of all posts with the same tags as the current post using the following code. However, I was then trying to add to the loop some logic t...
Get child terms of current term of the current post
I have custom post type "product" and taxonomy is "product_cat". There are two child terms -Music and videos. Under Music i have two more child terms albums and single. Now when i go to music category i want to get all the child terms of the music. I am using the following code But this is giving me the Music category and one child i.e albums . But...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.