How to get WooCommerce product by different properties

This article will explore the various ways to retrieve WooCommerce product objects. Efficiently getting WooCommerce products is important for managing an online store in WordPress. This article explores different ways and tools available in WooCommerce to access product information.

WooCommerce stores products as posts under a custom post type labeled “product.” This information becomes particularly useful when advanced product searches surpass the functionalities provided by WooCommerce. In such cases, the fact that products are stored as custom post types allows for implementing custom-tailored searching methods.

woocommerce get product

Getting WooCommerce product by ID

The best way to get a product object from product ID is by using the wc_get_product() function.

$product_id = 123;
$product = wc_get_product($product_id);

The function accepts the product ID as a first parameter and returns an instance of WC_Product class. The function will return false if a product with the given ID does not exist. Therefore, it’s a good practice to check if the product exists before working with the $product object to avoid errors further down the code:

$product = wc_get_product($product_id);
if ($product !== false){
  echo $product->name;
}

Getting WooComerce product by SKU

If you need to retrieve WooCommerce products by SKU, you can use the wc_get_product_id_by_sku() function. The function returns only the product ID, so this is a two-step process:

$sku = 'ABC-12345';
$product_id = wc_get_product_id_by_sku($sku);
$product = wc_get_product($product_id);

We first use the wc_get_product_id_by_sku() function to retrieve the product ID in WooCommerce associated with the provided SKU. In the next step, we use the wc_get_product() function to fetch the product object using the obtained $product_id.

Getting WooCommerce product by slug

The product slug in WooCommerce is a URL identifier representing a specific product, used for easy access and navigation within an online store.

WooCommerce doesn’t support a method that directly finds a product by its slug. However, knowing that products are stored as custom post types, we can use some of the WordPress core functions to help us achieve that. We can first find the single product page by its slug using the WordPress get_page_by_path() function. Once we have the product post, we can use the wc_get_product() function to retrieve the product with the given post ID. We can use this approach because the post ID and the product ID are the same identification number.

$slug = 't-shirt';
$product_post = get_page_by_path($slug, OBJECT, 'product');
$product = wc_get_product($product_post->ID);

Getting WooCommerce product by name

Getting a WooCommerce product by name is not as straightforward as the abovementioned methods. This is because the product name is not unique, and therefore, multiple products can exist with the same name. To find a single product by name we can use the wc_get_products() function, which returns a list of products based on the given criteria. After that, we need to ensure that the result contains exactly one product.

$product_name = 'T-shirt';
$products = wc_get_products( array('name' => $product_name) );
if(sizeof($products)==1){
  $product = $products[0];
}else{
  // handle 0 and more than 1 products
}

Here is a step-by-step explanation of how this code works:

  1. We first retrieve an array of WooCommerce product objects using wc_get_products(), filtering the products based on the name ‘T-shirt’.
  2. On the next step we check if there is exactly one product found that matches the name ‘T-shirt’ by verifying the size in the $products array. If that’s the case, we assign the first product object to the variable $product.
  3. If the number of products found is not exactly one (zero or more than one), it enters the else block. This is where we can handle scenarios where either no products or more than one product matches the name. It is important to handle both scenarios where 0 and more than 1 elements are returned to avoid unexpected behavior in your code.

Unlock the Full Potential of Your WooCommerce Customer Data

Using the WC_Product object

Once we have retrieved the product object, we can access various product data using the product properties. Here are some of the most commonly used attributes to get product info:

  • Get the product ID:
$product->get_id();
  • Get product name:
$product->get_name();
  • Get product slug:
$product->get_slug();
  • Get product SKU:
$product->get_sku();
$product->get_price();
  • Get product type (e.g. simple product, variable product)
$product->get_type();

WooCommerce Product Reports

Following our exploration of WooCommerce product retrieval methods, if you’re seeking comprehensive product reporting capabilities, Users Insights  product reporting feature emerges as a potent tool, offering enhanced analytics and insights to optimize your WooCommerce store management. It allows you to search a product by name and find valuable information about product sales, frequently bought together products and product variation sales. To learn more, you can visit the WooCommerce product reports page.
WooCommerce product sales reports

Conclusion

In conclusion, this article has provided a comprehensive overview of retrieving WooCommerce products using attributes such as ID, name, slug, and SKU. The PHP code discussed in this article can be used to enhance both plugin and theme functionality. This diverse range of retrieval approaches can help you to efficiently access and manipulate product data within your WooCommerce website and its product pages.