The Developer’s Guide to the WooCommerce $product Object

When working inside WooCommerce templates or custom functions, the `$product` object is your most valuable asset. It is the gateway to all metadata associated with an item in your store. Since the release of WooCommerce 3.0, "CRUD" (Create, Read, Update, Delete) methods have become the standard for accessing this data. Instead of accessing properties directly, developers now use specific getter functions. This guide serves as a quick-reference cheat sheet for the most essential methods to retrieve IDs, SKUs, pricing, and availability from any product object.

Get WooCommerce Product Details via $product Object

Hooks Woocommerce WooCommerce Custom Code Woocommerce Hooks WooCommerce Tips Wordpress WordPress Code Snippets WordPress Development WordPress Snippets WordPress Theme Development WordPress Tips

Get WooCommerce Product Details via $product Object Tutorial/Guide

In WooCommerce, the $product object allows you to retrieve various product details such as product ID, SKU, price, and more. Below is a comprehensive list of common methods to get essential product information:  

Methods to Retrieve Product Information

  1. Product ID: $product->get_id() - Returns the unique product ID.
  2. Product Name: $product->get_name() - Fetches the name/title of the product.
  3. Product Type: $product->get_type() - Returns the product type (simple, variable, external, etc.).
  4. Product SKU: $product->get_sku() - Retrieves the product's SKU (Stock Keeping Unit).
  5. Product Description: $product->get_description() - Fetches the full description of the product.
  6. Product Short Description: $product->get_short_description() - Gets the short description for the product.
  7. Product Regular Price: $product->get_regular_price() - Retrieves the regular price of the product.
  8. Product Sale Price: $product->get_sale_price() - Fetches the sale price if the product is on sale.
  9. Product Stock: $product->get_stock_quantity() - Returns the available stock quantity.
  10. Product Weight: $product->get_weight() - Fetches the product's weight.
  11. Product Dimensions: $product->get_dimensions() - Retrieves the dimensions (length, width, height) of the product.
  12. Product Categories: $product->get_category_ids() - Fetches a list of category IDs the product belongs to.
  13. Product Tags: $product->get_tag_ids() - Returns a list of tag IDs associated with the product.
  14. Product Images: $product->get_gallery_image_ids() - Retrieves the gallery image IDs for the product.
  15. Product Featured Image: $product->get_image_id() - Gets the ID of the featured image for the product.
  16. Product Attributes: $product->get_attributes() - Returns an array of the product's attributes.
  17. Product Variations: $product->get_available_variations() - Fetches available variations for variable products.
  18. Product Rating: $product->get_average_rating() - Retrieves the product's average rating.
  19. Product Review Count: $product->get_review_count() - Returns the number of reviews the product has.
  20. Product URL: $product->get_permalink() - Retrieves the product's page URL.
  21. Product Add to Cart URL: $product->add_to_cart_url() - Gets the URL to add the product to the cart.
  22. Product Add to Cart Text: $product->add_to_cart_text() - Retrieves the "Add to Cart" button text.

Explore Further

For additional advanced features and functions, visit the official WooCommerce Documentation. For a deeper dive into the $product object, use var_dump() or print_r() to examine all available properties and methods.

External Resources

πŸ’‘ Have a Coding Problem?

Search our archives or reach out to our team for solutions and expert advice.