How to Get Product Name WooCommerce

WooCommerce Get Product NameIf you want a quick and easy way to display WooCommerce product name, I have a quick WooCommerce get product name code snippet that I use from time to time to display the product name. You can use this code snippet in most of your WooCommerce theme development or WooCommerce plugin development tasks.

I will also show you how you can use this code to display product names in a list from a specific category. This is one of the most common ways of customizing the display of products in WooCommerce custom layout templates.  Let us cut the chase and look at how to display WooCommerce product name.

WooCommerce Get Product Name

Before you get the product name in WooCommerce, you need to understand that to get the product name in WooCommerce you need to have a number of parameters that you can use to get the product name.

  • First, the most common way to get the product name in WooCommerce is using the ID.
  • You can also get the product name using the slug of the product
  • You can get the product names using the category
  • You can get the product name using taxonomy
  • You can get product name using custom post meta and much more

There are dozens of logic you can use to get the product name depending on your project. For illustration in this tutorial, I will guide you on the way to get the product name using the ID.

WooCommerce Get Product Name By Id

To get the product name by the ID, you need to first get the ID of the product. You can get the ID of the product using the global product or post object as shown in the sample code below :

[php] global $post;

echo $post->ID;
[/php]

Once you have obtained the product ID you can now you can use the get_the_title() WordPress function to get the title and display the title as you wish. The following is the code that you should use to get WooCommerce product name and display it in the header as shown on the image below:

[php] add_action(‘wp_head’, ‘display_example_code_header_’);

function display_example_code_header_(){

global $post;

//echo $post->ID;

$product_title = get_the_title( $post->ID );

echo $product_title;
}

[/php]

woocommerce get product name 1

Conclusion

In this post, we have looked at the ways to get product name and display it. If you are looking for ways to customize this logic further you can get in touch with me for more insights and help or any other WooCommerce development tasks.

Similar Articles