WooCommerce: How to get the Product Name

The Problem

In WooCommerce, product details are stored in product objects that hold product data. If you want to get the name of a WooCommerce product then you will first need to create a product object then call the get_name() function on that object to retrieve the product name.

You will typically be in one of the following scenarios when you need to access a product object

Learn the basics of WooCommerce Development, in a friendly and accessible way
Click here for more details about the "Learning WooCommerce Development By Example" book
  • You have global variable available that contains the product object
  • You have the WooCommerce product id of the product you wish to get the name of but no global variable
  • You want to get a product name from the products in the cart
  • You want to retrieve a product name from the products in an order

In this post we’ll go through the code you will need to get the product name in each of the listed scenarios

Code to get the Product Name from a Global Product Object

global $product;

// Check if the global $product variable is a product object 
if ( ! is_a( $product, 'WC_Product' ) ) {
   //the $product variable is not a product object
}

echo $product->get_name();

In the code above we firstly call global $product, this will cause PHP to search the global namespace for a variable named $product and it finds one it will assign it’s value to the $product variable.

We then check the $product variable using the PHP function is_a to check that it’s an instance of the WC_Product class (all product variables in WooCommerce will typically be an instance of, or inherit from, the WC_Product class).

If the $product variable is an instance of the WC_Product class then we call echo to output the value of the get_name() function to the screen, this prints the product title.

When is the Product Global Object Likely to be Populated

WooCommerce will typically populate the $product global object on pages that deal with a single product, the single product page is probably the best example of this. The global may also be populated within code that is looping though a selection of products, for example the code on the shop category and archive pages.

In a scenario where the $product variable has not been populate but you do have the product id, you should use the code below.

Code if you Have the id of the Product you wish to get the Name of but no Global Variable

$product_id = 42;
$product = wc_get_product($product_id);
echo $product->get_name();

This code use the wc_get_product function to return a product object, the wc_get_product function takes a product id argument.

Once we have the product object returned from the function then we can call the get_name function to return the product name.

Code if you want to get a Product Name from the Products in the Cart

foreach( WC()->cart->get_cart() as $cart_item ) {
    echo $cart_item['data']->get_name() . '
'; }

The code above loops through each item in cart and outputs the product name for each item.

WooCommerce allows us to access the current user’s cart on any page by using the cart function of the global WC() function. It is then possible to call the get_cart() function to get a collection of the cart items, the collection can be looped over using foreach.

The foreach loop assigns the value of the current cart item to the $cart_item variable, the $cart_item variable is an array of items and the product object for each cart item can be accessed in the data element of the array. Once we have the product object we can call the get_name function as we did in the previous code samples.

Code if you want to get a Product Name from the Products in an Order

$order_id = 375;
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $order_item ) {
    echo $order_item ->get_name() . "
"; // Alternatively $product = $order_item ->get_product(); echo $product->get_name() . "
"; }

To access an order object you can use the wc_get_order function, the function takes an order id as an argument and returns an order object.

The code then calls the get_items() function on the order object that returns a collection of the order items that can be looped over using foreach.

The foreach loop supplies a variable named $order_item which is an instance of the WC_Order_Item_Product class. The WC_Order_Item_Product class has function named get_name() that returns the name of the product.

For the purposes of this example we only need the get_name() function, but if you did want to do something that involved a product object you can call the get_product() function on the $order_item object which will return a product object.


Warning: Undefined array key "format" in H:\root\home\bravenewworld101-001\www\hardworkingnerd\wp-content\plugins\convertkit\includes\class-convertkit-resource-forms.php on line 330

Leave a Comment