An overview of the WC_Product class in WooCommerce

This article thoroughly explores the WooCommerce WC_Product class, its most commonly inheriting classes, and the available methods and properties. The WC_Product class represents a product within the WooCommerce plugin. It is a fundamental object handling product data in WordPress, including properties like price, availability, variations, and other essential product attributes. This class can be used to manage and manipulate product information, enable functionalities such as displaying products, handle transactions, and implement customizations within the WooCommerce store.

wc_product class

The WC_Product class explained

The ≈ class in WooCommerce is an abstract class, serving as a blueprint for other more specific product classes. It is not designed to be instantiated as an object but provides essential properties and methods shared by various product types. Different product types, such as simple, variable, grouped, and external products, inherit from WC_Product, extending its functionalities to cater to their specific characteristics.

The most common classes that inherit from WC_Product include:

  • WC_Product_Simple: Represents a basic, single item without variations.
  • WC_Product_Variable: Handles products with multiple variations, such as size, color, or other attributes.
  • WC_Product_Grouped: Manages a collection or group of related products sold together.
  • WC_Product_External: Represents products listed externally, redirecting customers to another website for purchase.

Each of these classes extends the WC_Product abstract class, inheriting its core functionalities while introducing specialized features tailored to their respective product types. As a result, these classes share common methods and properties from the WC_Product class, and this article will delve deeper into their specifics.

As a result, these classes share common methods and properties from the WC_Product class, and this article will delve deeper into their specifics.

Creating an instance of WC_Product class

As mentioned above, the WC_Product class is not designed to be instantiated. However, you can create an instance using one of its inheriting classes or use one of the WooCommerce methods to create the instance for you.

Unlock the Full Potential of Your WooCommerce Customer Data

Creating an instance for existing (persisted) products

If you need to create a WC_Product object for an existing product stored in the database, the easiest way is to use the wc_get_product() function. This function accepts a product ID and searches the database to find a product with the given ID. If a product is found, it automatically returns an instance using the correct subclass depending on the product type.

Here is an example using a simple product:

$product_id = 123;
$product = wc_get_product($product_id);
echo get_class($product); //WC_Product_Simple
echo get_parent_class($product); //WC_Product

Creating an instance for new product objects

If you need to create an instance manually, you can directly instantiate the WC_Product subclasses. Here is an example of instantiating a simple product object. In this example, we also set the name of the product:

$product = new WC_Product_Simple();
$product->set_name('My simple product');

The following example shows how to create an instance for a variable product.

$product = new WC_Product_Variable();
$product->set_name('My variable product');

Retrieving data from WC_Product object

The WC_Product class provides various getter methods to retrieve different aspects of the single product information. These methods can be useful for managing, displaying, and customizing products efficiently within the WooCommerce store. The following sections will explore some of the most common methods used to retrieve product information.

General Product Information methods

    • get_id(): Retrieves the product’s ID.
    • get_name(): Fetches the product name.
    • get_slug(): Gets the product’s slug (part of the URL).
    • get_description(): Retrieves the product description.
    • get_short_description(): Fetches the product short description.
    • get_date_created(): Retrieves the date when the product was created.
    • get_date_modified(): Fetches the date when the product was last modified.
    • get_status(): Retrieves the product’s status (e.g., publish, draft).
    • get_type(): Fetches the product type (e.g., simple, variable, grouped).
    • is_type(string $type): Checks whether the product is from the given WC type.
    • get_sku(): Gets the product SKU (Stock Keeping Unit).
    • is_featured(): Checks if the product is featured.
    • is_visible(): Checks if the product is visible in the store.

Here is an example that uses these methods:

$product_id = 123;
$product = wc_get_product($product_id);
$product->is_type('variable'); // true of false

Pricing methods

These methods provide information about WooCommerce product pricing, such as regular and sale prices of the product:

  • get_price(): Returns the price of the product that is currently active.
  • get_regular_price(): Retrieves the regular price of the product.
  • get_sale_price(): Fetches the product’s sale price (if on sale).
  • is_on_sale(): Returns whether or not the product is on sale.

The following example illustrates how to use these methods:


Product stock methods

These methods fetch stock-related details such as quantity available and stock management status in the WordPress store:

  • get_stock_quantity(): Retrieves the available stock quantity of the product.
  • get_stock_status(): Returns the stock status of the product (e.g., in stock, out of stock).
  • get_manage_stock(): Fetches whether stock management is enabled for the product.
  • get_backorders(): Returns the backorders of the product.
  • is_in_stock(): Checks whether the product is in stock.
  • is_purchasable(): Checks whether the product can be purchased.
  • is_on_backorder(): Checks whether the product is on backorder.

Here is an example of using these methods:


Tax and Shipping methods

This category involves methods related to tax and shipping information associated with the product, including tax status, tax class, and assigned shipping class ID.

  • get_tax_status(): Retrieves the tax status of the product.
  • get_tax_class(): Get product the tax class.
  • get_shipping_class_id(): Retrieves the shipping class ID assigned to the product.
  • is_taxable(): Checks whether the product is taxable.
  • is_shipping_taxable(): Checks if the product’s shipping is taxable

And here is how you can get the WooCommerce product tax information:


Product dimension methods

The WC_Product class also provides several methods allowing you to retrieve the product dimensions:

    • get_dimensions(): Retrieves the dimensions (length, width, height) of the product.
    • get_weight(): Gets the weight of the product.
    • get_width(): Gets the width of the product
    • get_height(): Gets the height of the product

The following example illustrates how you can retrieve the WooCommerce product dimensions:


Product Links and Add-to-cart methods

  • get_permalink(): Retrieves permalink (URL) of the product page.
  • get_add_to_cart_url(): Returns the add to cart URL that can be used to add the product to the cart.
  • add_to_cart_text(): Returns the add to cart button text.

Category and tag methods

The WC_Product class provides getter methods for retrieving the WooCommerce product taxonomies – this includes both product categories and product tags:

  • get_category_ids(): returns the IDs of the the product categories
  • get_tag_ids(): returns the IDs of the product tags

Here is how you can use these methods:


Variations and attributes methods

These methods can be used for variable products, providing access to attributes, default attributes, variation IDs, and variation descriptions.

  • get_attributes(): Retrieves the product’s attributes.
  • get_default_attributes(): Returns the default attributes of the product.
  • get_children(): Returns the child products or variations associated with a variable product.

The following snippet shows how you can use these methods:


Downloadable product methods

These methods handle aspects related to downloadable products in WooCommerce, providing access to download file URLs, download expiry settings, and limits on the number of downloads allowed for a product’s downloadable files.

  • is_downloadable(): Checks if the product is downloadable
  • get_downloads(): Retrieves an array containing downloadable file URLs associated with the product.
  • get_download_expiry(): Returns the download expiry period for the product’s downloadable files in days.
  • get_download_limit(): Retrieves the maximum number of times the downloadable files can be downloaded after purchase.

How to get WooCommerce product meta

In WooCommerce, product metadata can be accessed using the get_meta() method from the WC_Data class. Here’s an example of how you can retrieve the value associated with the given meta key:

$product = wc_get_product($product_id);
$meta_value = $product->get_meta('your_meta_key');

Updating and saving WooCommerce products

The WC_Product class also provides various methods allowing to update product attributes. For almost all of the above-mentioned getter methods, a corresponding setter method is available. For example, to get the product name, we can use the get_name() method. If we want to set the product name, we can use the set_product() name method.

The following example illustrates how to update some of the product attributes:

$product = wc_get_product($product_id);

// Update product attributes
$product->set_name('New Product Name');
$product->set_description('Updated product description.');
$product->set_category_ids(array(8, 12)); // Replace category IDs accordingly

// Save changes to the product

This example showcases how to update a WooCommerce product by setting various attributes such as the product name, regular price, description, stock quantity, and category IDs. It uses the corresponding setter method for each attribute. Finally, the save() method is used to persist the changes in the database.

Retrieving WooCommerce product analytics

While the WC_Product class provides some general product information, it is also important for every store manager to understand the product sales performance. The Users Insights Product Sales Reports feature offers insightful data on product sales, frequently bought items, and variation sales. For additional information, you can visit to the WooCommerce product reports page.

WooCommerce product sales reports


The WC_Product class within WooCommerce offers a range of commonly used methods for accessing vital product information. By exploring these methods, we’ve gained insights into retrieving essential details such as product names, pricing, SKU, stock availability, and other attributes. These methods are fundamental tools to manage, display, and customize product data within the WooCommerce shop, enabling efficient handling and presentation.