WooCommerce Hooks: Retrieving Single Product Data (ID, SKU, Prices) From the $product Object

WooCommerce Tutorial

Retrieving Product Data (ID, SKU, Prices) From the $product Object in WooCommerce – When developing custom features or extensions in WooCommerce, a common requirement is accessing product data to retrieve essential details like the product ID, SKU, price, and other metadata.

WooCommerce Hooks: Get Product Data (ID, SKU, $) From $product Object

WooCommerce provides a powerful $product object, which holds all the information related to a product.

This object includes methods to retrieve product details such as pricing, stock status, tax info, dimensions, and much more.

TL;DR

Hide
  • WooCommerce provides a powerful $product object that holds all information related to a product, including methods to retrieve product details like pricing, stock status, and tax info.
  • The product ID is a unique identifier that allows various operations on the product, and can be retrieved using the get_id() method.
  • General product info like type, name, slug, creation and modification dates, and SKU can be retrieved using methods like get_type(), get_name(), get_slug(), get_date_created(), and get_sku().
  • Product permalink can be retrieved using the get_permalink() function, which generates the product's URL based on its ID.
  • Pricing details like regular price, sale price, and sale date range can be retrieved using methods like get_regular_price(), get_sale_price(), and get_date_on_sale_to().
  • Tax data, shipping settings, and stock levels can be retrieved using methods like get_tax_class(), get_stock_quantity(), and get_backorders().
  • Product dimensions like weight, length, width, and height can be retrieved using methods like get_weight(), get_length(), get_width(), and get_height().
  • Linked product data like upsells, cross-sells, and variations can be retrieved using methods like get_upsell_ids(), get_cross_sell_ids(), and get_variation_ids().
  • Product variations and attributes can be retrieved using methods like get_children(), get_attributes(), and get_default_attributes().
  • Product categories and tags can be retrieved using methods like wc_get_product_category_list(), get_category_ids(), and get_tag_ids().
  • Digital product data like download links, expiration dates, and download counts can be retrieved using methods like get_downloadable_file_urls(), get_download_limit(), and get_download_count().
  • Product images and galleries can be retrieved using methods like get_image(), and get_gallery_images().
  • Customer reviews and ratings can be managed and displayed using methods like get_review_count(), and get_average_rating().

In this article, we will cover how to retrieve various types of data from the $product object using its built-in methods.

The same methods I use when I built OneClick Chat to Order and all its add-ons such as OneClick WCFM Connector, OneClick Dokan Connector, and OneClick Variations Grabber.

We will look into general product info, pricing, stock details, tax information, and even related products. Each section will demonstrate how to extract this data programmatically.

Here’s the WooCommerce hooks for single product on how to 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 Product Taxonomies
wc_get_product_category_list( $product_id, $sep = ', ' );
$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();

Getting the Product ID and General Information

The product ID is one of the most fundamental pieces of information when working with WooCommerce products.

It serves as the unique identifier that allows you to perform various operations on the product, such as retrieving additional metadata, managing the product via the admin interface, and more.

To retrieve the product ID, use the following method:

$product->get_id();

This will return the product’s unique ID, which you can use for further operations.

Retrieving General Product Info

Besides the product ID, the $product object allows you to retrieve various general details about the product, such as its type, name, slug, creation and modification dates, and SKU. Here are some of the most common methods for getting general product information:

$product->get_type(); // Get product type (simple, variable, etc.)
$product->get_name(); // Get product name
$product->get_slug(); // Get product slug (URL-friendly name)
$product->get_date_created(); // Get product creation date
$product->get_date_modified(); // Get product modification date
$product->get_status(); // Get product status (e.g., publish, draft)
$product->get_featured(); // Check if the product is featured
$product->get_catalog_visibility(); // Get catalog visibility (visible, hidden, search)
$product->get_description(); // Get full product description
$product->get_short_description(); // Get short product description
$product->get_sku(); // Get product SKU (Stock Keeping Unit)
$product->get_menu_order(); // Get the product’s menu order
$product->get_virtual(); // Check if the product is virtual (non-physical)

Each method returns a specific property of the product, which you can use as needed. For example, you can retrieve the product name or SKU for displaying on the front end or for other processing.

You can also retrieve the product’s permalink using the following function:

get_permalink($product->get_id());

This retrieves the product’s URL based on its ID, which can be useful for generating links or redirecting users.


Working with Product Prices

Price management is a key feature when working with eCommerce platforms like WooCommerce.

The $product object allows you to easily retrieve various pricing details, including the regular price, sale price, and even the date range during which a sale is active.

Retrieving Product Prices

Here are the methods for retrieving product prices:

$product->get_price(); // Get the current price of the product
$product->get_regular_price(); // Get the regular (non-sale) price
$product->get_sale_price(); // Get the sale price (if applicable)
$product->get_date_on_sale_from(); // Get the date when the sale starts
$product->get_date_on_sale_to(); // Get the date when the sale ends
$product->get_total_sales(); // Get the total number of sales for the product
  • get_price() will return the current price of the product, which could be the sale price if a discount is active.
  • get_regular_price() gives the standard price, while get_sale_price() returns the discounted price if a sale is running.
  • get_date_on_sale_from() and get_date_on_sale_to() provide the sale’s start and end dates, which can be useful for time-limited promotions.

Product Tax, Shipping, and Stock Information

WooCommerce products can have various tax classes and shipping settings based on your store configuration.

Additionally, products may be in or out of stock, and stock levels need to be monitored.

Retrieving Tax, Shipping, and Stock Information

Here are methods to get the necessary data related to tax, shipping, and stock:

$product->get_tax_status(); // Get the product’s tax status (e.g., taxable, none)
$product->get_tax_class(); // Get the product’s tax class (if applicable)
$product->get_manage_stock(); // Check if stock management is enabled
$product->get_stock_quantity(); // Get the current stock quantity
$product->get_stock_status(); // Get the stock status (in stock, out of stock)
$product->get_backorders(); // Check if backorders are allowed
$product->get_sold_individually(); // Check if the product can be sold individually
$product->get_purchase_note(); // Get the product purchase note
$product->get_shipping_class_id(); // Get the shipping class ID

These methods allow you to retrieve the product’s stock status, such as whether it’s in stock or backorders are allowed. You can also check tax status and class, which are crucial when calculating the total price during checkout.

  • get_stock_quantity() returns the current stock level.
  • get_backorders() will let you know if backorders are allowed, which can help manage pre-orders or out-of-stock scenarios.

Handling Product Dimensions

For physical products, dimensions such as weight, length, width, and height are important attributes, especially for shipping purposes.

Retrieving Product Dimensions

To retrieve these details, use the following methods:

$product->get_weight(); // Get product weight
$product->get_length(); // Get product length
$product->get_width(); // Get product width
$product->get_height(); // Get product height
$product->get_dimensions(); // Get all dimensions as a string

These methods return the product’s physical dimensions, which can be displayed to customers or used to calculate shipping costs.


Linked Products: Upsells, Cross-sells, and Parent Products

WooCommerce allows you to link products as upsells, cross-sells, or variations (in case of a variable product). These linked products can increase conversions and average order value.

Retrieving Linked Products

You can retrieve linked product data with the following methods:

$product->get_upsell_ids(); // Get upsell product IDs
$product->get_cross_sell_ids(); // Get cross-sell product IDs
$product->get_parent_id(); // Get the parent product ID (useful for variations)
  • Upsells are products that are recommended instead of the current product, encouraging customers to purchase a higher-end item.
  • Cross-sells are related products often suggested on the cart page to complement the current product.

Product Variations and Attributes

In WooCommerce, products can have variations and attributes (such as size, color). This is especially important for stores selling clothing or customizable products.

Retrieving Product Variations and Attributes

$product->get_children(); // Get the IDs of variations (for variable products)
$product->get_attributes(); // Get all attributes of the product
$product->get_default_attributes(); // Get default attributes (for variable products)
$product->get_attribute('attributeid'); // Get a specific attribute value

For variable products, get_children() will return an array of variation IDs, which you can use to retrieve details about each variation.

get_attributes() gives a list of all product attributes, while get_default_attributes() returns default selections (e.g., default size or color).


Taxonomies: Categories and Tags

Products in WooCommerce can be associated with categories and tags, helping organize and categorize items.

Retrieving Product Taxonomies

wc_get_product_category_list($product_id, $sep = ', '); // Get product categories
$product->get_category_ids(); // Get category IDs
$product->get_tag_ids(); // Get tag IDs

These methods return product categories and tags. wc_get_product_category_list() provides a formatted list of categories, while get_category_ids() and get_tag_ids() return arrays of category and tag IDs.


Managing Product Downloads

If you’re selling digital products, you’ll need to manage downloads, download limits, and expiration dates.

Retrieving Product Download Information

$product->get_downloads(); // Get download files associated with the product
$product->get_download_expiry(); // Get the number of days before downloads expire
$product->get_downloadable(); // Check if the product is downloadable
$product->get_download_limit(); // Get the limit on the number of downloads

These methods help manage downloadable products by retrieving information on file downloads, expiration limits, and download counts.


Handling Product Images

Product images are crucial for eCommerce. WooCommerce makes it easy to retrieve both the main product image and the gallery of additional images.

Retrieving Product Images

$product->get_image_id(); // Get the ID of the main product image
$product->get_image(); // Get the main product image HTML
$product->get_gallery_image_ids(); // Get the IDs of gallery images

These methods allow you to retrieve the main product image and gallery images, which can then be displayed on product pages or elsewhere in your store.


Working with Product Reviews

Customer reviews and ratings are an important part of any online store. WooCommerce allows you to enable or disable reviews and retrieve review-related data for each product.

Retrieving Product Reviews Information

$product->get_reviews_allowed(); // Check if reviews are allowed for the product
$product->get_rating_counts(); // Get rating count per star level
$product->get_average_rating(); // Get the average rating of the product
$product->get_review_count(); // Get the total number of reviews

These methods let you manage and display customer reviews, which can help build trust with potential buyers.


Final Thoughts

WooCommerce’s $product object is a powerful tool that provides access to a wealth of product data, from basic information like the product ID and SKU to more advanced data like pricing, stock levels, tax settings, and linked products.

Understanding how to retrieve this data programmatically is essential for customizing and extending WooCommerce’s functionality.

By leveraging these methods, developers can build custom solutions tailored to specific eCommerce needs, enhancing both the front-end and back-end experiences.

Related Posts