I’ve been wanting to publish this guide for a long while. As a WooCommerce development freelancer, every day I repeat many operations that make me waste time… and one of them is indeed “How to get ____ if I have the $product variable/object?“.
For example, “How can I get the product SKU“? Or “How can I get the product short description“? Or maybe the product stock level, shipping class, tax class, price, regular price, sale price, and so on… hopefully this article will save you time.
Of course, not always you have access to the $product global – but you may know the $product_id. In this case, you can use the wc_get_product WooCommerce function to calculate the $product object – you find this case scenario below.
Other examples might be the order or the cart page. Once again, in here you don’t really have a $product available, so you have to loop through the order/cart items and “get” it. After that, you can then calculate and get any piece of information you require out of $product. Enjoy!
1. You have access to $product variable
Hooks (do_action and apply_filters) use additional arguments which are passed on to the function. If they allow you to use the “$product” object you’re in business. Alternatively, you can declare the “global $product” inside your function.
In both cases, here’s how to get all the product information:
// Get Product ID
$product->get_id();
// Get Product General Info
$product->get_type();
$product->get_name();
$product->get_slug();
$product->get_date_created();
$product->get_date_modified();
$product->get_status();
$product->get_featured();
$product->get_catalog_visibility();
$product->get_description();
$product->get_short_description();
$product->get_sku();
$product->get_menu_order();
$product->get_virtual();
get_permalink( $product->get_id() );
// Get Product Prices
$product->get_price();
$product->get_regular_price();
$product->get_sale_price();
$product->get_date_on_sale_from();
$product->get_date_on_sale_to();
$product->get_total_sales();
// Get Product Tax, Shipping & Stock
$product->get_tax_status();
$product->get_tax_class();
$product->get_manage_stock();
$product->get_stock_quantity();
$product->get_stock_status();
$product->get_backorders();
$product->get_sold_individually();
$product->get_purchase_note();
$product->get_shipping_class_id();
// Get Product Dimensions
$product->get_weight();
$product->get_length();
$product->get_width();
$product->get_height();
$product->get_dimensions();
// Get Linked Products
$product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();
// Get Product Variations and Attributes
$product->get_children(); // get variations
$product->get_attributes();
$product->get_default_attributes();
$product->get_attribute( 'attributeid' ); //get specific attribute value
// Get Product Taxonomies
wc_get_product_category_list( $product_id, $sep = ', ' );
$product->get_category_ids();
$product->get_tag_ids();
// Get Product Downloads
$product->get_downloads();
$product->get_download_expiry();
$product->get_downloadable();
$product->get_download_limit();
// Get Product Images
$product->get_image_id();
$product->get_image();
$product->get_gallery_image_ids();
// Get Product Reviews
$product->get_reviews_allowed();
$product->get_rating_counts();
$product->get_average_rating();
$product->get_review_count();
2. You have access to $product_id (use wc_get_product to get $product)
If you have access to the product ID (once again, usually the do_action or apply_filters will make this possible to you), you have to get the product object first. Then, do the exact same things as above.
// Get $product object from product ID
$product = wc_get_product( $product_id );
// Now you have access to (see above)...
$product->get_type();
$product->get_name();
// etc.
// etc.
3. You have access to the Order object or Order ID
How to get the product information inside the Order? In this case you will need to loop through all the items present in the order, and then apply the rules above.
// Get $product object from $order / $order_id
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product = $item->get_product();
// Now you have access to (see above)...
$product->get_type();
$product->get_name();
// etc.
// etc.
}
If you wish to expand your knowledge, here’s an other article on how to get additional info out of the $order object.
4. You have access to the Cart object
How to get the product information inside the Cart? In this case, once again, you will need to loop through all the items present in the cart, and then apply the rules above.
// Get $product object from Cart object
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item_key => $cart_item ){
$product = $cart_item['data'];
// Now you have access to (see above)...
$product->get_type();
$product->get_name();
// etc.
// etc.
}
If you wish to expand your WooCommerce PHP knowledge, here’s an other article on how to get additional info out of the $cart object.
5. You have access to $post object
In certain cases (e.g. the backend) you can only get access to $post. So, how do we “calculate” $product from $post? Easy peasy:
// Get $product object from $post object
$product = wc_get_product( $post );
// Now you have access to (see above)...
$product->get_type();
$product->get_name();
// etc.
// etc.
Hi Rodolfo,
I have a question about how the $product variable works, if I were to add a new table to my WP database (MySQL) what table would I use add a Foreign Key and to join the data from my new table to the correct table for this kind of operation. For example right now I want to create a specs table to use and tie that data to products after I perform an SQL join in my php script.
Thanks a lot,
– Anthony
Not sure I fully understood, anyhow if you’re creating a table that you want to “join” to the posts/products one, you need to make sure one of its columns is the post/product ID
I just ended up making two separate tables and pulled product data dynamically with php for data entry on the admin side.
thanks for the help though.
Alright, cool!
Hi Rodolfo,
WC_Product::get_categories is deprecated and I got the suggestion to use wc_get_product_category_list
maybe you will update this?
Thanks for your great work here.
Andi
Done, much appreciated!
This was soo helpful. Always find myself coming back to this page whenever I use WC.
Great to hear!
You have done an amazing job here! Thank for the resources – it’s a gold mine for WC coding.
Cheers
Can you tell me how to get variable subscription product variations in shortcode?
Hello Ahmad, you can use get_children()
Hi, does any one now how to display a list of EAN’s from a variable product on the product page? So nut just once but a complete list.
Do you mean SKUs?
Yes, for example from a variation product:
Productname: SKU:
Watch Red 0000001
Watch White 0000002
Watch Black. 0000003
And also for simple products:
Productname: SKU:
Bike. 0000001
I see. You could reuse some of this code: https://www.businessbloomer.com/woocommerce-display-variations-sku-product-admin/
this article save my life :”)
Great!
Hello Rudolph,
I am preparing a snippet to be able to send sales data to the Data Layer and be able to use them as variables in Google Tag Manager, I have something like what I put below:
I have managed to identify most of these variables, except for the affiliate ID and the product (item) brand
Do you know what they would be? or better, where to see a relationship of all the variables?
Thanks
Hola Carlos, it really depends on how those plugins (brand and affiliate) save data. This project is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Is there a way to change the word “Product” in WooCommerce? Or just on the single product page? I have a client who books art classes. She asked me to change Products on it to Classes.
https://www.businessbloomer.com/translate-single-string-woocommerce-wordpress/
Great cheatsheet but many of these are obsolete. Are you planning on updating it?
Which ones are you referring to Fan?
Is there any way to get a specific variation price when a product attribute is selected?
For Example,
A shirt with 3 sizes, each with a different price. Is there any way to get the different price when one of the attributes is selected?
Hi Jon, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Does this somehow also work with WC Bookings?
the Bookings email has get_id() ); ?> however, when I try for example $booking_get_shipping_address() it results in an error, also none of the above works to call customer details.
Nope, this is default WooCommerce. Each plugin has its own “getters”
One more:
Nice!
While I install some plugin, it shows “post was called incorrectly. Product properties should not be accessed directly” error.
May I ask where I should put “$product->get_id()” code?
It should be fixed by the plugin developer – please contact them
Hello,
I am finding a difficulty, I want to get LearnDash Course ID from WooCommerce product. My LearnDash is integrated with WooCommerce as a closed course. Is there any way to find the course ID from the attached WooCoomerce product id?
Your help will save me.
Thanks in advance.
Hi Nayan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo!
There’s a way to display or to get all the product summary via php?
You mean the “short description”?
Hi Rodolfo. I want to thank you for this article. Very straight to the point. Cheers
Awesome
Thank you
Well done
Cheers
How to get product brand?
Depends on which plugin you use for brands
`$product->get_categories()` is deprecated. Use `wc_get_product_category_list( $product->get_id()`
Cheers!
you may use
and you got all product`s categories id, use it whatever to achieve your goal
Cheers
please can you guide me how can i get the refunded products’ category
as i use : $product->get_categories(); in loop it gives me error.
Hey Muhammad, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
¿Como podría obtener los atributos en una tabla ?
Es decir lo que aparece en aditional information
Marcel, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hey there! I had a quick question for you. I am selling a “Variable Product” on my store, however I am currently only selling one option. Therefore, instead of “Select Options” I need the button to say “Add to Cart”. Is there a code I can use to change that? Also, just curious where I should place my code?
Thank you for your time!
Hi William, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Good one
Cheers!
On the WooCommerce store page where the full list of products appears, I want to get the ID of each product to be able to add a form button and directly catch the ID of each product. I tried to insert into the functions.php:
and
But, either it doesn’t load the page or it returns the page ID of the store, not the product where I click.
Hi Solhein, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Great Work Rodolfo,
Saves me some minutes every time I look at this page. Hats off.
Cheers,
That’s the goal! Thank you
Hi im try to get wc_product_download url file but i can’t, could you help me
Maybe:
This continues to be a fantastic resource for me. Thank you for posting this.
Great! How can I improve it?
Very helpful article Rodolfo, Can you please tell me how I can add a buy now button to my WooCommerce single product page, is it possible I use a dynamic URL to get product Id and automatically add it to cart and redirect to checkout? Please help. Thanks (ps: I want to be able to add the buy now button anywhere on the single product page)
Hi Abubakar, you could use a variation of https://businessbloomer.com/woocommerce-continue-shopping-button-single-product-page/
Thanks for this great list! I’m trying to get just the ProductID, but when I use get_id() it gives me back a TON of other info…like Product Name, Sku, etc etc Do I need to add a parameter to the get_id() to get JUST the ProductID?
No, it should work like that. What code are you using?
For some reason, now it’s working! I literally changed nothing from
Thanks for checking.
Good!
hello Rodolfo,
Thanks for this great list, this should be in woocommerce docs !!
I tried it and it works well for me, except for something maybe too particular.
i want to echo the “smaller thumbnail url” of each products in my “home made Cart viewer”
its only for a preview so size matters.
When i echo get_image() it returns the
When i echo get_the_post_thumbnail_url($product_id) it returns the url of the original uploaded and not thumbnails array list
my code
Do you have a trick to return thumbnails url list or something that can help me in this way..
Thanks 🙂
missing part : When i echo get_image() it returns the img with src, srcset and sizes
Nico, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Super Hiflreich! Great Resource, thanks for sharing!!
Great!
WC()->cart->get_cart();
in my website seems not being recognized.
WooCommerce version?
I’d also like to find the Version.
Not working anymore.
I suppose they changed sintax with latest version.
What’s not working Salvatore?
how can i get total no of products which i have set initially..
eg: i have set 100 in stock
after each order it gets decreasing but i need initial total value of the product as i want to show it like this..
x/y left
x=remaining products
y=total products
please help
Hi Tanish, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid this is custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Variation and Simple:
Thank you!
Brilliant article, addresses all the core issues I’ve been searching for, for days! Thank you**********
Excellent!
Hi,
How can I get product terms (category name only)?
Thanks!
Hi Giang, thanks for your comment! You could use $product->get_category_ids() and then turn IDs into names 🙂
Superb article. 🙂
Thank you!
Is it possible to get currency symbol in this way ? I’m using $product->get_price() but it return price without symbol, how can I pull Symbol. I use currency switcher and try to automated short description for each product. Your post helped me a lot. I just missing this currency symbol to be pulled automatically.
Hey Przemyslaw, thanks for your comment! Try using get_woocommerce_currency_symbol(), this will give you what you want 🙂
I read on SO that if you want to display a price, you need to wrap your price in a
function.
not sure if i am allowed to share the link here
Is that okay if I get all the data separately (Title, Thumbnail, Price etc) and create my own template for WP template with WC?
Of course Thas 🙂 Good luck!
Hero!
Thank you 🙂
How can i get $product->get_categories(); without link only name?
Hello Saagar – thanks so much for your comment! Once you get the categories, you can use something similar to this https://developer.wordpress.org/reference/functions/get_categories/#comment-333 to loop through the categories and just echo the name. Hope this helps
While I install some plugin, it shows “post was called incorrectly. Product properties should not be accessed directly” error.
May I ask where I should put “$product->get_id()” code?
Hey Robin, thanks so much for your comment! It could be a plugin that is out of date, try disabling them one at a time to see which one is causing the error. Hope this helps!
Number 3 was what I needed. Perfect!
Thank You
Excellent 🙂
Nice Cheat Sheet…
I think there are different method to make the list more complete, like getting categories linkes with
$product->get_categories()
let’s make the list more complete.. 😀
Awesome, thanks Viktor 🙂
Great article!
smth function from me:
Thanks Tusko!
also `$p->is_type(‘variation’)` is useful for iterating through product variations.
Hi, im trying to do the next snippet:
but isn’t working, i don’t know if it fails because its cart page or i can’t use this variables here.
Thanks for the help and sorry for my bad english (:
Hola Sergi, thanks so much for your comment! Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R
Number 1 & 2 broke but #3 worked for me using the StoreFront template.
Thanks for all your help!!!!
Steve
Uhm, ok, these are independent from the theme, but thank you for your feedback anyway Steve 🙂