Retrieve WooCommerce Product Information (ID, SKU, Price) from the $product Object
In WooCommerce, the $product
object represents a single product and provides access to a wide range of product-related information. Using the $product
object, you can easily retrieve details such as product ID, SKU, price, stock status, and more. Below is a list of common methods and properties available to get the most important product details:
Common Methods to Retrieve Product Information
- Product ID:
$product->get_id()
– Retrieves the unique ID of the product. - Product Name:
$product->get_name()
– Retrieves the product name/title. - Product Type:
$product->get_type()
– Retrieves the product type (simple, variable, grouped, or external). - Product SKU:
$product->get_sku()
– Retrieves the SKU (Stock Keeping Unit) for the product. - Product Description:
$product->get_description()
– Retrieves the detailed description of the product. - Product Short Description:
$product->get_short_description()
– Retrieves the short description of the product. - Product Regular Price:
$product->get_regular_price()
– Retrieves the regular price of the product. - Product Sale Price:
$product->get_sale_price()
– Retrieves the sale price if the product is on sale. - Product Stock Quantity:
$product->get_stock_quantity()
– Retrieves the current stock quantity available for the product. - Product Weight:
$product->get_weight()
– Retrieves the weight of the product. - Product Dimensions:
$product->get_dimensions()
– Retrieves the dimensions (length, width, height) of the product. - Product Categories:
$product->get_category_ids()
– Retrieves an array of category IDs to which the product belongs. - Product Tags:
$product->get_tag_ids()
– Retrieves an array of tag IDs assigned to the product. - Product Gallery Images:
$product->get_gallery_image_ids()
– Retrieves an array of gallery image IDs. - Product Featured Image:
$product->get_image_id()
– Retrieves the featured image ID of the product. - Product Attributes:
$product->get_attributes()
– Retrieves an array of product attributes. - Product Variations (for variable products):
$product->get_available_variations()
– Retrieves available variations for variable products. - Product Rating:
$product->get_average_rating()
– Retrieves the average rating of the product. - Product Reviews Count:
$product->get_review_count()
– Retrieves the number of reviews for the product. - Product URL:
$product->get_permalink()
– Retrieves the URL to the product’s page. - Product Add to Cart URL:
$product->add_to_cart_url()
– Retrieves the URL to add the product to the cart. - Product Add to Cart Text:
$product->add_to_cart_text()
– Retrieves the text for the ‘Add to Cart’ button.
These methods will help you extract essential information about your WooCommerce products. Keep in mind that additional custom properties or methods might be available depending on the plugins or themes you have installed on your WooCommerce store.
Explore Further
For more in-depth information and advanced functionality, refer to the official WooCommerce Documentation.
If you want to inspect the $product object in your own setup, use var_dump()
or print_r()
to explore all available properties and methods.