How to Get Product Info (Price, ID, Name, Etc) from WooCommerce $product Object

You are currently viewing How to Get Product Info (Price, ID, Name, Etc) from WooCommerce $product Object

The WooCommerce $product object holds every piece of data that we need to work with products.

If you’re a WooCommerce developer, you’ll need to access some or all of that information at some point or another.

Whether you’re developing a plugin, theme, or just a code snippet, knowing how to get the relevant product information is the key to success.

Once you access the $product variable, you can use any class object method.

Some of the product data you can get from the $product object is the item name, order, ID, SKU, price, stock level, order notes, etc. We can retrieve complete information about the item simply by getting the $product object.

For example, when a customer asks us to create a code snippet to disable a payment method for a specific product, we need to check if the item exists on the cart page and conditionally execute the function based on true or false.

This was just one scenario out of many possibilities, and you can find many more in some of the most useful WooCommerce functions.

Now that we know how essential the product object is let’s discover how to get the data from it.

Get Data from the $product Variable

Some additional arguments are passed through the function when working with WooCommerce hooks (actions and filters).

If you can use the $product object as an argument in the function, you’re golden. However, if the process doesn’t accept it as an argument, you’ll need to define the ‘global $product’ inside it to access all the object’s methods.

Once you’re ‘in,’ here is the product’s data you can get.

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:

WooCommerce extensions

$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 Tax, Shipping, and 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 Similar / Linked Products:

$product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();

Get Variations and Attributes:

$product->get_children(); // get variations
$product->get_attributes();
$product->get_default_attributes();
$product->get_attribute( 'attributeid' );

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();

No $product Variable? No Worries!

If the hooks don’t accept the $product variable as is, you can still get to the object by adding another step.

The following sections will show how to retrieve the $product object from different variables and objects.

Access the $product Object from the $product_id

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

Access the $product Object from the $order_id

$order = wc_get_order( $order_id );
$items = $order->get_items();
  
foreach ( $items as $item ) {
  
    $product = $item->get_product();
    
    $product->get_type();
    $product->get_name();
    // etc.
  
}

Access the $product Object from the Cart Object

$cart = WC()->cart->get_cart();
  
foreach( $cart as $cart_item_key => $cart_item ){
  
    $product = $cart_item['data'];
    
    $product->get_type();
    $product->get_name();
    // etc.
  
}

Access the $product Object from the $post Object

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

Once you get into the $product object, you can use any of its methods from the first section.

Related Articles

  1. How to set a default variation in WooCommerce
  2. How to reorder products in WooCommerce
  3. How to hide the WooCommerce additional information tab
  4. WooCommerce: add back in stock notifications
  5. Best name your price plugins for WooCommerce
  6. How to change store currency in WooCommerce
  7. Add additional variation images in WooCommerce
  8. How to change the sale badge in WooCommerce

Conclusion

This article showed you how to access the WooCommerce $product object and use its data to expand the functionality of the WooCommerce plugin.

You can use it to develop a new plugin, theme, extension, or simple function.

Leave a comment and tell us what you use the $product object for or if you need further help.

Also, make sure to subscribe to our YouTube channel and like our page on Facebook.

PluginsForWP

PluginsForWP is a website specializing in redistributing WordPress plugins and themes with a variety of knowledge about WordPress, internet marketing, and blogging.

Leave a Reply