WooCommerce Guide: Using the $product Object

Estimated reading time: 5 minutes
Category: WordPress

Now let’s look at one of the most commonly used objects in WooCommerce development, the $product object, which provides a wide range of methods for accessing product data.

Fetching a Product

To retrieve a WooCommerce product, you can use the wc_get_product() function with the product ID:

$product = wc_get_product( $product_id );

// Example usage
echo $product->get_name(); // Gets the product name

If you want to fetch a product by SKU, you can use the following method:

$product_id = wc_get_product_id_by_sku( $sku );
$product = wc_get_product( $product_id );

// Example usage
if ( $product )
    echo $product->get_name();

Getting Product Categories

You can retrieve the categories a product belongs to using:

$product_categories = wp_get_post_terms( $product_id, 'product_cat' );

foreach ( $product_categories as $category )
    echo $category->name; // Outputs the category name

Alternatively, WooCommerce provides a method:

echo wc_get_product_category_list( $product_id );

Retrieving Product Meta

WooCommerce stores custom data in post meta. You can access this data using $product->get_meta()
This method fetches product metadata stored by WooCommerce:

$meta_value = $product->get_meta( 'my_meta_key' );
echo $meta_value;

Key points about get_meta() method:

  • Retrieves metadata specific to WooCommerce’s structured product data.
  • Works well with meta added via WooCommerce or custom plugins using the WooCommerce API.

Or you can use get_post_meta(), this is a WordPress core function used to retrieve post meta for any post type:

$meta_value = get_post_meta( $product_id, 'my_meta_key', true );
echo $meta_value;

Key points about get_post_meta() function:

  • Works for all post types, including products, pages, and posts.
  • Fetches raw meta values stored in the database.

Differences:

  • $product->get_meta() may process or filter meta values before returning them, based on WooCommerce’s internal handling.
  • get_post_meta() directly retrieves raw meta values from the database.

When to use which?

  • Use $product->get_meta() for WooCommerce-specific metadata.
  • Use get_post_meta() for generic or custom metadata not managed by WooCommerce.

Get Product General Info

Access the basic information about the product:

$product->get_id();                  // Product id
$product->get_type();                // Product type (e.g., 'simple', 'variable')
$product->get_name();                // Product name
$product->get_slug();                // Product slug (URL-friendly name)
$product->get_date_created();        // Date the product was created
$product->get_date_modified();       // Date the product was last modified
$product->get_status();              // Product status (e.g., 'publish', 'draft')
$product->get_featured();            // Check if the product is featured
$product->get_catalog_visibility();  // Visibility (e.g., 'visible', 'hidden')
$product->get_description();         // Full product description
$product->get_short_description();   // Short description
$product->get_sku();                 // Stock-keeping unit (SKU)
$product->get_menu_order();          // Menu order (for sorting)
$product->get_virtual();             // Check if the product is virtual
get_permalink( $product->get_id() ); // Get product permalink

Get Product Prices

Retrieve pricing information:

$product->get_price();               // Current price (regular or sale)
$product->get_regular_price();       // Regular price
$product->get_sale_price();          // Sale price
$product->get_date_on_sale_from();   // Start date of the sale
$product->get_date_on_sale_to();     // End date of the sale
$product->get_total_sales();         // Total sales for the product

Get Product Tax, Shipping & Stock

Access tax, shipping, and stock-related details:

$product->get_tax_status();          // Tax status (e.g., 'taxable')
$product->get_tax_class();           // Tax class
$product->get_manage_stock();        // Whether stock is managed
$product->get_stock_quantity();      // Stock quantity
$product->get_stock_status();        // Stock status (e.g., 'instock', 'outofstock')
$product->get_backorders();          // Backorder status
$product->get_sold_individually();   // Whether sold individually
$product->get_purchase_note();       // Purchase note
$product->get_shipping_class_id();   // Shipping class ID

Get Product Dimensions

Retrieve physical dimensions:

$product->get_weight();              // Weight
$product->get_length();              // Length
$product->get_width();               // Width
$product->get_height();              // Height
$product->get_dimensions();          // Full dimensions as a formatted string

Get Linked Products

Retrieve related product links

$product->get_upsell_ids();          // Array of upsell product IDs
$product->get_cross_sell_ids();      // Array of cross-sell product IDs
$product->get_parent_id();           // Parent product ID (for variations)

Get Product Variations and Attributes

Work with product variations and attributes

$product->get_children();            // Array of variation IDs
$product->get_attributes();          // Array of product attributes
$product->get_default_attributes();  // Default attributes for variable products
$product->get_attribute( 'attribute_slug' ); // Specific attribute value ('pa_color' or 'color')

Get Product Taxonomies

Retrieve category and tag information:

wc_get_product_category_list( $product_id, $sep = ', ' ); // Category names
$product->get_category_ids();        // Array of category IDs
$product->get_tag_ids();             // Array of tag IDs

Get Product Downloads

Access downloadable product details:

$product->get_downloads();           // Array of download files
$product->get_download_expiry();     // Download expiry (in days)
$product->get_downloadable();        // Whether the product is downloadable
$product->get_download_limit();      // Number of allowed downloads

Get Product Images

Retrieve product images:

$product->get_image_id();            // ID of the main product image
$product->get_image();               // URL of the main product image
$product->get_gallery_image_ids();   // Array of gallery image IDs

Get Product Reviews

Access reviews and ratings:

$product->get_reviews_allowed();     // Whether reviews are allowed
$product->get_rating_counts();       // Array of rating counts (per star)
$product->get_average_rating();      // Average rating
$product->get_review_count();        // Number of reviews

That’s it! With these basic methods, you can easily create custom WooCommerce solutions and display product information however you need.

Leave a Reply

Your email address will not be published. Required fields are marked *