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

I’ve been wanting to publish this guide for a long while. As a 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 🙂

1. You have access to $product

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(); (fixes the error: "Notice: id was called incorrectly. Product properties should not be accessed directly")

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

$product->get_attributes();
$product->get_default_attributes();

// Get Product Taxonomies

$product->get_categories();
$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

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 = new WC_Order( $order_id );
$items = $order->get_items();

foreach ( $items as $item ) {

    $product = wc_get_product( $item['product_id'] );

    // Now you have access to (see above)...

    $product->get_type();
    $product->get_name();
    // etc.
    // etc.

}

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 ){

    $product = wc_get_product( $cart_item['product_id'] );

    // Now you have access to (see above)...

    $product->get_type();
    $product->get_name();
    // etc.
    // etc.

}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

47 thoughts on “WooCommerce: Easily Get Product Info (ID, SKU, $) from $product Object

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

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

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

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

    1. Great!

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

        in my website seems not being recognized.

        1. WooCommerce version?

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

    1. What’s not working Salvatore?

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

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

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

    1. Excellent!

  9. 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 🙂

  10. Superb article. 🙂

    1. Thank you!

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

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

  13. Hero!

    1. Thank you 🙂

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

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

  16. Number 3 was what I needed. Perfect!

    Thank You

  17. 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 🙂

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

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

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

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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