Retrieving WooCommerce Product Names Outside the Single Product Page
Displaying a product title on an arbitrary pageâlike a landing page, cart summary, or a custom gridârequires understanding how WooCommerce stores and retrieves data. The core concept is straightforward: every product in WooCommerce is essentially a WordPress post of type product. Consequently, you can access product details using standard WordPress functions, provided you have the correct identifier.
In your specific scenario, you identified that the URL parameter ?post=129 corresponds to the product ID. This assumption is correct. WooCommerce relies on the standard WordPress post meta system, where the ID field in the database holds the numeric identifier for the product. When you view the admin edit page or a single product URL, this number represents the unique key used to fetch data from the database.
To echo the product name using PHP within a custom shortcode or template file, you do not need to write a complex query manually. The WordPress API offers a dedicated function for exactly this purpose: wc_get_product(). This function accepts the product ID and returns an object containing all available attributes of that product.
Here is the minimal implementation to retrieve and display the name:
// Assume $product_id is 129 based on your URL analysis
$product = wc_get_product( $product_id );
if ( $product ) {
echo $product->get_name();
}
This approach is preferred over accessing global variables or relying on the current loop because it explicitly targets the specific product ID you possess. The wc_get_product() function returns a WC_Product object, which acts as a facade for the underlying data. Calling get_name() on this object retrieves the value stored in the "Product Name" field of the database.
Handling Fallbacks and Empty States
A critical pitfall when retrieving product names is handling cases where the ID might be invalid or the product name is not set. In WooCommerce, if a product exists but has no name entered in the admin panel, the default behavior of get_name() is to return a placeholder string like "Untitled Product".
If you are building a feature where an empty name breaks the layout or confuses the user, you should implement a fallback check. You can compare the returned name against common defaults or use the get_type() method to ensure you are actually dealing with a valid product object before rendering it.
$product = wc_get_product( $product_id );
if ( $product && ! empty( $product->get_name() ) ) {
echo '<h2>' . esc_html( $product->get_name() ) . '</h2>';
} else {
// Optional: Handle missing name gracefully
echo 'Product not found or untitled';
}
Using esc_html() is essential here. Even though you are echoing data retrieved from the database, always sanitize output before rendering it to prevent XSS vulnerabilities if user-generated content is involved in future updates.
Contextual Considerations for Shortcodes
You mentioned using a PHP plugin shortcode like [php]echo 'example';[/php]. While this works for simple output, wrapping your logic inside a shortcode allows you to pass arguments directly from the frontend without hardcoding IDs.
function display_product_name_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => 0,
), $atts ) );
$product = wc_get_product( $id );
if ( ! $product ) {
return '';
}
return '<strong>' . esc_html( $product->get_name() ) . '</strong>';
}
add_shortcode( 'product_title', 'display_product_name_shortcode' );
By registering a shortcode, you can simply place [product_title id="129"] in any page content. This decouples the logic from the display layer and makes it easier to manage multiple product references across your site.
If you require more advanced data manipulation or custom taxonomies, consider leveraging the wc_get_product_data_store() methods for deep database access, but for standard name retrieval, the WC_Product object remains the most robust and maintainable solution.
