Posted on

WooCommerce – How to Get $Product ID, SKU, Price from $Product Object?

Discover how to get WooCommerce product ID, SKUs and price via code snippets. And, also see how to bulk manage WooCommerce products from one place.

WooCommerce - Get product ID, SKU, and price from $product

Last updated on November 28, 2024

Are you wondering how to get a WooCommerce product ID, SKU, or details like regular price, sale price, stock, shipping class, tax class, images, dimensions, attributes and categories?

Accessing these product details is essential for various store operations, such as identifying products in an order, applying advanced filters, updating prices or managing stock.

This guide provides simple code snippets to fetch product data by ID or SKU, along with other key information to streamline your stock management.

You’ll also discover how to manage WooCommerce products, orders, coupons and other WordPress custom post types – all from a single dashboard.

Ready to optimize your WooCommerce store productivity? Let’s get in!

Getting product info from $product object via code

Sometimes, the $product object isn’t readily available. Depending on your use case, you may need to retrieve it in a different way.

For instance, if you have a $product_id but no $product object, you’ll need to use the ID to fetch the product.

This situation often arises on the order or cart pages, where the $product object isn’t directly accessible. In such cases, you’ll need to loop through the order or cart items to retrieve it.

Once you have the $product object, you can easily extract any details you need, such as price, SKU or 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. 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

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

For a better understanding, here’s what you can check out – How to get additional info out of the $order object.

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.

If you wish to expand your WooCommerce PHP knowledge, here’s another article on how to get additional info out of the $cart object.

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

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.

How do I find WooCommerce product ID without coding?

You can also get WooCommerce product ID using three simple ways:

WordPress backend

  1. Log in to your WordPress Admin panel dashboard.
  2. Click on Products > All Products.
  3. Hover on each product row and you’ll get the product ID.
get product id from WordPress backend

Edit product URL

Open any product to edit. You’ll find the product ID in the URL as “post=X”.

product id in url

Smart Manager plugin

The first two methods can be quite time-consuming – you’ll either need to open each product individually or search for a product and hover over it to find its ID.

With Smart Manager, however, all your product information – ID, stock, price, SKU and more – is available in one place.

You’ll have access to a ton of details in an easy-to-navigate, Excel-like sheet editor. You can save a lot of time and streamline your workflow by managing everything from a single interface!

WooCommerce get product ID sku price category details at one place

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:

  • Add any number of new products to your stock database directly.
  • Enable or disable the ‘Manage stock’ for each product.
  • Manage and edit SKU, stock status, regular price and sale price for each.
  • Edit product description and tax status.
  • Add Tax status, Tax class, Shipping class, features images and product thumbnails for each product. Add attributes, edit categories and product status.
  • Increase or decrease sale price by X% or amount of regular price for all or selected stock.
  • Bulk edit stock status, inventory and more.
  • Set the sale price based on the product’s regular price using the bulk edit feature.
  • Perform operations like append, prepend, increase, decrease, set to, copy from, etc. using bulk edit.
  • 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.
  • Filter stock based on stock status, SKU, price, etc. Apply OR, AND or a combination of both conditions to fetch desired results amongst thousands of items.
  • Get predefined custom views. View only required stock-related data columns and hide all other columns to focus only on your stock or inventory-related metrics. Also sort products by name, SKU, price, etc. before making an export.
  • Export only those stock data as CSV which you need. Like the name and stock quantity. These will let people at your shop or warehouse know which product needs quick replenishment, which product stock is slow-moving, etc.
  • Delete individual stock, using filters and using bulk edit. Learn more about how to delete all products safely.
  • Manage stock fields added by custom plugins.
  • Manage and bulk edit posts and pages – status, title, content, categories, tags, SEO status and keywords.
  • Smart Manager also helps bulk delete your products based on filters like test products, permanently or move them to trash. You can do the same for orders, coupons, pages and other post types.

Smart Manager is compatible with these top WooCommerce plugins – WooCommerce Subscriptions, Bookings, Memberships, Product Add-Ons and Cost of Goods.

You can easily manage all stock and product fields added by these plugins. Like products, you can also manage and bulk edit orders, coupons, posts, pages, users and any WordPress post type.

Try Smart Manager live demo

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.

Leave a Reply

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.