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 code?

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 delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

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?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

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.

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

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

    1. What’s not working Salvatore?

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

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

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

    1. Excellent!

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

  6. Superb article. 🙂

    1. Thank you!

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

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

  9. Hero!

    1. Thank you 🙂

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

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

  12. Number 3 was what I needed. Perfect!

    Thank You

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

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

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

  16. 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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.