Ever felt like pulling your hair out trying to find out which ‘Awesome Blue T-shirt’ a customer ordered when all you have is a mysterious product ID?
Or perhaps you are trying to show those much-needed SKUs on your shop page without breaking anything? Well, this is the crazy world of WooCommerce product data!
Getting quick access to product IDs, SKUs, pricing, attributes, categories and inventory levels is not only helpful but utterly necessary to run a prosperous online shop.
This guide serves up bite-sized code snippets that will give you an instant retrieval of these crucial pieces of information using either product IDs or SKUs.
Now imagine processing orders faster than lightning, filtering your product catalog like a pro, and updating price or stock management“. This is precisely what we are going to help you achieve in just a few lines of code, even a coding novice can implement.
Shall we move from sad to glad in our WooCommerce workflow? Let’s get started!
Why do you need a product ID
Even though you usually don’t need to see a product’s special number (called the product ID) when you’re making a product and showing it on your website using its regular link, there are still some good reasons why you might want to know what that number is.
- For instance, you would maybe like to show one particular product on another page of your website, perhaps in a sidebar or special section. For this, you might use a little special code (short code). In most cases, that short code would require indication of which product to show, referring to its product ID.
- While creating a listing for your products in WordPress, perhaps to display according to arrangement or grouped together, a request might come out for the product ID through which to ascertain which items would be included in your list.
- What may need the very special filter for your products to be viewed by the people- for instance, to only show products of certain colors and sizes? This advanced filtering might require you to apply the filter to certain product IDs.
So, having outlined why knowing a particular product ID is useful for these situations, let us now look into the means of finding the WooCommerce product ID inside your online shop.
Getting product info from $product object via code
Sometimes, you might not have the product details ($product
object) right away. You might need to get them in a different way, depending on what you’re doing.
For example:
- If you only have the product’s ID (
$product_id
): You’ll need to use that ID to go and get all the product details. - On order pages or shopping cart pages: You often don’t have the product details directly. In these cases, you need to go through each item in the order or the cart to find the product details.
Once you finally have the product details ($product
object), it’s easy to find things like:
- The price
- The product code (SKU)
- How many are in stock
You have access to $product variable
When you have access to the $product variable, you can easily retrieve product details.
Hooks like do_action and apply_filters often pass additional arguments to your function. But:
- If these include the $product object, you’re in luck – you can use it directly.
- If not, you can still access the product information by declaring “global $product” inside your function to make the $product variable available.
In both scenarios, you’ll be able to retrieve all the product details you need, such as SKU, price, stock, and more. Here’s how you can do it:
// 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 $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();
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 for 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.
You have access to the Order object or Order ID
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. }
Accessing product information within orders is incredibly beneficial. By looping through order items and retrieving product details, you can:
- Create detailed invoices as complete product specifications
- Add important factors such as SKU, weights, and dimensions to shipping
- Add special handling instructions for fragile or heavy items.
- Calculate lead times for custom manufacturing of products
- Measuring precise locations inside a warehouse for an item
- Automated shipping recommendations based on order content, etc.
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. }
Imagine you’re building a special message that appears in the shopping cart, like “Free shipping on orders with 3 or more t-shirts!”
To show a message like “Free shipping on 3+ t-shirts,” your code needs to check every item in the cart to count the t-shirts. Without this, it wouldn’t know what’s in the cart.
You have access to $post object
In certain cases (e.g. the WordPress admin side or backend) you can only get access to $post. So, how do we “calculate” $product from $post?
Here’s how:
// 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.
Imagine you’re in the WordPress admin, looking at a list of all your website pages and products. WordPress shows everything as a general “post.”
If you want to do something specific to a product on that list – like change its price or see its stock – you first need to tell WordPress, “Hey, this ‘post’ is actually a ‘product’!” The $post
object is the general info, and using wc_get_product($post)
is like saying, “Calculate the specific ‘product’ details from this general ‘post’ info.
How do I find WooCommerce product ID without coding?
You can also get WooCommerce product ID using three simple ways:
WordPress backend
- Log in to your WordPress Admin panel dashboard.
- Click on
Products > All Products
. - Hover on each product row and you’ll get the product ID.
Edit product URL
Open any product to edit. You’ll find the product ID in the URL as “post=X”.
Smart Manager plugin
While it might be tempting to dismiss endless clicking as just part of the job, these first two options really come down to opening every product or indulging in a game of hide-and-seek with your mouse.
But just imagine this: Smart Manager lays out all the important product details – ID, stock, price, SKU, and on and on – right in front of you in one easy-to-scan place.
Think of it as an all-mighty spreadsheet to manage your store. You have instant access to truckloads of information in a manner that makes sense. Say goodbye to wasting precious hours and hello to an altogether different, smoother, faster way of running your shop-from one simple screen!
How to make WooCommerce inventory management efficient with Smart Manager
Whether it’s simple, variable, or affiliate products, Smart Manager works seamlessly across all product types.
Here’s how Smart Manager with its Excel-like sheet editor simplifies product and inventory management:
- Eliminate the tedious one-by-one product editing in WooCommerce, especially for attributes, with its powerful bulk editing features and improved filtering.
- Manage and edit SKU, stock status, products, prices and categories for each.
- Easy WooCommerce inventory control for all product types.
- Quickly find products/variations by SKU (simple or advanced search).
- Bulk add/ directly edit/delete products by SKU.
- Easily modify SKUs via duplication.
- Vendors can search SKUs and manage stock.
- Sort/export products by SKU.
- Find orders/subscriptions by SKU.
- See only what you need by hiding other columns and easily sort products by name, SKU, or price before exporting.
- Bulk remove or delete categories of products from WordPress.
- Simplify WooCommerce shipping class management for stores with many products, allowing you to quickly assign classes to all or specific products using filters.
- Easily export all your WooCommerce product data (including variations, custom fields, categories, prices, stock, and images) to CSV with flexible filtering.
- Increase sales by setting strategic sale prices based on regular prices with bulk edit.
- Manage product data efficiently with bulk operations like adding, changing, or copying information.
- Add attributes to hundreds of products at once using the bulk edit feature.
- Real-time stock updates – stock quantity updates automatically when the sale is made.
- Manage backorders.
Like products, you can also manage and bulk edit orders, coupons, posts, pages, users and any WordPress post type.
Conclusion
If you love coding and are well-versed with hooks and filters, you can take the coding approach to get product info as and when required.
To view and manage hundreds and thousands of products from a single place, do bulk edit, export, delete, duplicate and other store operations quickly, use the Smart Manager plugin.
We are sure you won’t regret your purchase.
FAQ
How do I get the product by SKU in WooCommerce?
Go to WordPress admin > Products
. Click on any product to edit it. Scroll down and click on the Inventory tab to find or add the product SKU.
Can I find a product ID with an SKU number?
Yes.
How do I get the product image from a product ID in WooCommerce?
All you need to do is use the WordPress wp_get_attachment_url()
function. The wp_get_attachment_url()
function accepts an attachment ID as its only parameter. So, all you need to do is pass in the product ID and you’ll be able to get the URL of the product image.