Posted on

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

This article lists down some codes to get product ID, sku, price, category and other details in WooCommerce. And also how to manage all products from one place.

Last updated on January 11, 2022

How to get the product ID? How to get the product SKU? Or how to get the product price, stock, sale price, regular price, shipping class, tax class, images, dimensions, attributes, categories and more?

If you are a techie, I will phrase it as “How to get ____ if I have the $product object/variable?”

Fetching what products were present in an order, applying advanced product filters, editing prices and description…you need product details to accomplish various store operations.

This article lists down some codes you can use to get product by ID, product by category, product by price, product and other details in WooCommerce to tick off your stock management tasks.

And also, how you can get and manage all your product details, orders, coupons, any WordPress custom post type from one place, thus increasing your store productivity 10x.

So let’s begin.

Getting product info from $product object via code

Not always do you have access to the $product object (I’m talking about WooCommerce hooks for example). Understand your case and see if you can “get” that $product object in another way.

In this case, you have to find a way to “get the $product object from $product_id”.

Other examples might be the order page or the cart page. 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.

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, it’s great. Alternatively, you can declare the “global $product” inside your function.
In both cases, this is how you 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
  
$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 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.

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

Also read – 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.
  
}

Also read – How to get additional info out of the $cart object.

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

How to get all the product/stock at one place?

What if you want all your product info, stock, price, additional details, SKU…tons of details at one place?

Not just a view, but also edit details as and when required and that too in bulk?

Smart Manager makes it possible for you. It shows all your products’ data in a neat dashboard.

Featured images, regular price, sale price, additional information, SKU, categories, attributes…all readily available in an Excel-like spreadsheet. Manage, edit products directly using the spreadsheet. Hours of tasks within minutes or seconds.

Simple products, variable products and variations, affiliate products…works for all.

WooCommerce get product ID sku price category details at one place

How to better manage stock / inventory with Smart Manager?

  • Add any number of new products to your stock database.
  • Enable or disable the ‘Manage stock’ for each product.
  • Manage, edit SKU, stock status, regular price, sale price for each.
  • Edit product description, tax status.
  • Add Tax status, Tax class, Shipping class, features images, 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, etc.
  • Set sale price based on the product’s regular price using bulk edit.
  • Perform operations like append, prepend, increase, decrease, set to, copy from, etc. using bulk edit.
  • Add attributes to hundreds of products at once using bulk edit
  • Real-time stock updates – Stock quantity updates automatically when 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

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

You can easily manage all stock and product fields added by these plugins.

With such ease, don’t you feel Smart Manager is the best WooCommerce stock management plugin?

Try Smart Manager live demo

Conclusion

If you love coding, 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 Smart Manager plugin. You won’t regret your purchase.

Article reference – BusinessBloomer.com

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.