WooCommerce: Get Product Info (ID, SKU, $) From $product Object

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.

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

106 thoughts on “WooCommerce: Get Product Info (ID, SKU, $) From $product Object

  1. 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

    1. Done, much appreciated!

  2. This was soo helpful. Always find myself coming back to this page whenever I use WC.

  3. You have done an amazing job here! Thank for the resources – it’s a gold mine for WC coding.

  4. Can you tell me how to get variable subscription product variations in shortcode?

    1. Hello Ahmad, you can use get_children()

  5. 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.

      1. 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

  6. this article save my life :”)

  7. 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:

        currency: 'echo $order_currency;', 
    

    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

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

  8. 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.

  9. Great cheatsheet but many of these are obsolete. Are you planning on updating it?

    1. Which ones are you referring to Fan?

  10. 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?

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

  11. 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.

    1. Nope, this is default WooCommerce. Each plugin has its own “getters”

  12. One more:

    $product->get_price_html();
    
    1. Nice!

  13. 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?

    1. It should be fixed by the plugin developer – please contact them

  14. 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.

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

  15. Hi Rodolfo!

    There’s a way to display or to get all the product summary via php?

    1. You mean the “short description”?

  16. Hi Rodolfo. I want to thank you for this article. Very straight to the point. Cheers

    1. Awesome

  17. Thank you
    Well done

    1. Cheers

  18. How to get product brand?

    1. Depends on which plugin you use for brands

  19. `$product->get_categories()` is deprecated. Use `wc_get_product_category_list( $product->get_id()`

    1. Cheers!

      1. you may use

         $product->get_category_ids()

        and you got all product`s categories id, use it whatever to achieve your goal

        1. Cheers

  20. 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.

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

  21. ¿Como podría obtener los atributos en una tabla ?
    Es decir lo que aparece en aditional information

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

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

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

  23. Good one

    1. Cheers!

  24. 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:

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

    and

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

    But, either it doesn’t load the page or it returns the page ID of the store, not the product where I click.

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

  25. Great Work Rodolfo,

    Saves me some minutes every time I look at this page. Hats off.

    Cheers,

    1. That’s the goal! Thank you

  26. $product = wc_get_product();
    $downloads = $product->get_file();
    print_r($downloads);
    

    Hi im try to get wc_product_download url file but i can’t, could you help me

    1. Maybe:

      $files = $product->get_downloads();
  27. This continues to be a fantastic resource for me. Thank you for posting this.

    1. Great! How can I improve it?

  28. 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)

  29. 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?

    1. No, it should work like that. What code are you using?

      1. For some reason, now it’s working! I literally changed nothing from

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

        Thanks for checking.

        1. Good!

  30. 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

    foreach ( WC()->cart->get_cart() as $cart_item ) 
    			{
    			$image = $cart_item['data']->get_image();
    			echo $image;
    			$product_id =  $cart_item['data']->get_id() ;
    			echo get_the_post_thumbnail_url($product_id);
    			}
    

    Do you have a trick to return thumbnails url list or something that can help me in this way..
    Thanks 🙂

    1. missing part : When i echo get_image() it returns the img with src, srcset and sizes

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

  31. Super Hiflreich! Great Resource, thanks for sharing!!

    1. Great!

      1. WC()->cart->get_cart();

        in my website seems not being recognized.

        1. WooCommerce version?

          1. I’d also like to find the Version.

  32. Not working anymore.
    I suppose they changed sintax with latest version.

    1. What’s not working Salvatore?

  33. 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

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

  34. Variation and Simple:

    if( $cart_item['variation_id'] ){
    	$product = wc_get_product($cart_item['variation_id']);
    }else{
    	$product = wc_get_product($cart_item['product_id']);	
    }
    
    1. Thank you!

  35. Brilliant article, addresses all the core issues I’ve been searching for, for days! Thank you**********

    1. Excellent!

  36. Hi,

    How can I get product terms (category name only)?

    Thanks!

    1. Hi Giang, thanks for your comment! You could use $product->get_category_ids() and then turn IDs into names 🙂

  37. Superb article. 🙂

    1. Thank you!

  38. 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.

    1. Hey Przemyslaw, thanks for your comment! Try using get_woocommerce_currency_symbol(), this will give you what you want 🙂

    2. I read on SO that if you want to display a price, you need to wrap your price in a

      wc_price()

      function.

      $product_price  = wc_price($product->get_price());

      not sure if i am allowed to share the link here

  39. Is that okay if I get all the data separately (Title, Thumbnail, Price etc) and create my own template for WP template with WC?

    1. Of course Thas 🙂 Good luck!

  40. Hero!

    1. Thank you 🙂

  41. How can i get $product->get_categories(); without link only name?

    1. 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

  42. 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?

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

  43. Number 3 was what I needed. Perfect!

    Thank You

  44. 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.. 😀

    1. Awesome, thanks Viktor 🙂

  45. Great article!

    smth function from me:

    $product->get_title()
    $product->is_visible()
    $product->is_featured()
    $product->is_on_sale()
    $product->has_child()
    $product->get_variation_price( 'min', true )
    $product->get_variation_price( 'max', true )
    $product->get_variation_regular_price( 'min', true )
    $product->get_variation_regular_price( 'max', true )
    $product->is_type( $type ) //checks the product type, string/array $type ( 'simple', 'grouped', 'variable', 'external' ), returns boolean
    
    
    1. also `$p->is_type(‘variation’)` is useful for iterating through product variations.

  46. Hi, im trying to do the next snippet:

    function ventascruzadas(){
      $cart = WC()->cart->get_cart();
      global $product;
    foreach( $cart as $cart_item ){
     	
        $product = wc_get_product( $cart_item['product_id'] );
     
        // Now you have access to (see above)...
     	
        $cross = $product->get_cross_sell_ids();
        // etc.
        // etc.
     
    }
      echo "<h2>Puede que estés interesado en...</h2>";
      
      foreach($cross as $key => $value){
    	echo $value."<br>";
      }
    }
    

    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 (:

    1. 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

  47. Number 1 & 2 broke but #3 worked for me using the StoreFront template.

    Thanks for all your help!!!!
    Steve

    1. Uhm, ok, these are independent from the theme, but thank you for your feedback anyway Steve 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *