Woocommerce, get current product id

shparkison picture shparkison · Dec 9, 2014 · Viewed 168.1k times · Source

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!!

Answer

Samyer picture Samyer · Apr 18, 2017

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.