Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 646
shparkison Avatar asked Dec 09 '14 18:12

shparkison


People also ask

How do I find the current product ID in WooCommerce?

A second option is to head over the Products page in your WordPress Admin. In this listing, you'll find the WooCommerce product ID when you hover over a product name. You can additionally search for your product using the product SKU name or product name and hover over the search results to get the Product ID.

How do I get product SKU in WooCommerce?

Once you click on the edit option, you find all the available settings for that individual product. Scroll down and click on the inventory and there will be the option to set a WooCommerce product SKU.


2 Answers

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.

like image 117
Samyer Avatar answered Oct 01 '22 12:10

Samyer


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(); 
like image 45
helgatheviking Avatar answered Oct 01 '22 10:10

helgatheviking