• You MUST read the Babiato Rules before making your first post otherwise you may get permanent warning points or a permanent Ban.

    Our resources on Babiato Forum are CLEAN and SAFE. So you can use them for development and testing purposes. If your are on Windows and have an antivirus that alerts you about a possible infection: Know it's a false positive because all scripts are double checked by our experts. We advise you to add Babiato to trusted sites/sources or disable your antivirus momentarily while downloading a resource. "Enjoy your presence on Babiato"

Add Product ID to Product title Woocommerce [HELP]

Saint Gabriel

Well-known member
Trusted Uploader
Jan 3, 2020
3,187
3,331
113
Hi code and WordPress experts in the house.

Anyone knows how to add the product ID to new and existing product titles in Woocommerce?

For example,

Product: Bag
Product ID: 1234

Product title on publish or update of existing product:

Bag - 1234

Can this be done without any changes to the permalink structure?

Please advise, thanks.
 
Do you want to add permanently? Or just SEO title?
Hm.
Just to specify on the website.

We have products with similar names so we want to use randomly generated numbers to make them unique for the customers to identify.

And I figured since WordPress generates product IDs, we can use that.
 
@Saint Gabriel


You can create your own function to handle the product title. Put this code to function.php of your child theme:

HTML:
// For shop
function change_woocommerce_single_title($title, $id) {

    if ( in_array( get_post_type( $id ), array( 'product', 'product_variation') ) || is_product() )
      $title = $title . ' - ' . $id;
     
    return $title;
}

add_filter('the_title', 'change_woocommerce_single_title', 1, 2);

// For cart
function filter_woocommerce_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
   
    if( $cart_item['variation_id'] )
        $id = absint( $cart_item['variation_id'] );
    else
        $id = absint( $cart_item['product_id'] );
       
    $item_name = $item_name . ' - ' . $id;

    return $item_name;
   
};
       
add_filter('woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3);

Screenshot 2022-08-22 210719.png
 
Last edited:
  • Love
Reactions: Saint Gabriel
@Saint Gabriel


You can create your own function to handle the product title. Put this code to function.php of your child theme:

HTML:
// For shop
function change_woocommerce_single_title($title, $id) {

    if ( in_array( get_post_type( $id ), array( 'product', 'product_variation') ) || is_product() )
      $title = $title . ' - ' . $id;
  
    return $title;
}

add_filter('the_title', 'change_woocommerce_single_title', 1, 2);

// For cart
function filter_woocommerce_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
 
    if( $cart_item['variation_id'] )
        $id = absint( $cart_item['variation_id'] );
    else
        $id = absint( $cart_item['product_id'] );
    
    $item_name = $item_name . ' - ' . $id;

    return $item_name;
 
};
    
add_filter('woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3);

Screenshot 2022-08-22 210719.png

It worked like a charm! 🥳🥳🥳
Thank you!! @Proxybunker 🔥🔥🔥


UPDATE: It also crashed the website on the front end.

I
t triggers the error on the product page.
 
  • Like
Reactions: olalavui
The website is: *** Hidden text: cannot be quoted. ***
give this code a try however it will only work on the front-end it will not show in the basket or cart
HTML:
add_filter('the_title', 'change_woocommerce_single_title', 1, 2);

function change_woocommerce_single_title($title, $id) {

   if ( class_exists( 'woocommerce' ) && is_product())
      $title = $title . ' - ' . $id;
      
    return $title;
}
 
  • Love
Reactions: Saint Gabriel
give this code a try however it will only work on the front-end it will not show in the basket or cart
HTML:
add_filter('the_title', 'change_woocommerce_single_title', 1, 2);

function change_woocommerce_single_title($title, $id) {

   if ( class_exists( 'woocommerce' ) && is_product())
      $title = $title . ' - ' . $id;
      
    return $title;
}
Thanks. Or is there a way to delay the execution of a PHP code?

I tried using SKU in product title but the issue is the SKU auto generates SKU from product ID with another plugin then the code I got from stack overflow adds the SKU to the product title.

But when a product is published, it doesn't show the SKU in product title until you save again because the SKU plugin had not generated the SKU yet.

So I want to know if there's a way to make a PHP code delay execution.
 
Hi, I have a similar problem with a website I'm updating for an old client of mine.

They have their listing in an Excel sheet. They have a lot of items with the same name like pencils, bags, keychains, etc. Some names with the SKU as prefixes and others as suffixes. Like: "P567 PEN" or "PEN P567" What I did is separate each one into its own column: "SKU = P567" and "Name = PEN" in the SKU and name column and separate each.

The same procedure you can do merging the "SKU" and "Name" columns and your name + SKU is your new title for every product. without changing the URL.
  1. Export your products from WooCommerce
  2. Fix the SKU and Name columns
  3. Merge the SKU and Name columns
  4. Import your product to WooCommerce
I hope this works for you!
 
Last edited:
Hi, I have a similar problem with a website I'm updating for an old client of mine.

They have their listing in an Excel sheet. They have a lot of items with the same name like pencils, bags, keychains, etc. Some names with the SKU as prefixes and others as suffixes. What I did is fix the SKU and name column and separate each one.

The same procedure you can do merging the SKU and Name collum and your name + SKU is your new title for every product. without changing the URL.

  1. Export your products from WooCommerce
  2. Fix the SKU and Name columns
  3. Merge the SKU and NAme collums
  4. Import your product to WooCommerce
I hope this works for you!
I don't understand, please can you share a screenshot? Or record a video of what you're saying with loom?
 
Sorry I edited my answer for a better explanation!

WooCommerce has an "Import - Export" feature in the Products menu. I'm working directly on that .CSV file for adding new products to WooCommerce.

I'm waiting for my client's final list of products to proceed to fix that Excel file into the .CSV WooCommerce import file.

I do a fast search on YouTube and find this video:
Tutorial - How To Import or Update WooCommerce Products in Bulk With a CSV file

Let me know if you need more help!
 
Sorry I edited my answer for a better explanation!

WooCommerce has an "Import - Export" feature in the Products menu. I'm working directly on that .CSV file for adding new products to WooCommerce.

I'm waiting for my client's final list of products to proceed to fix that Excel file into the .CSV WooCommerce import file.

I do a fast search on YouTube and find this video:
Tutorial - How To Import or Update WooCommerce Products in Bulk With a CSV file

Let me know if you need more help!
This won't work.
 
give this code a try however it will only work on the front-end it will not show in the basket or cart
HTML:
add_filter('the_title', 'change_woocommerce_single_title', 1, 2);

function change_woocommerce_single_title($title, $id) {

   if ( class_exists( 'woocommerce' ) && is_product())
      $title = $title . ' - ' . $id;
     
    return $title;
}
Please any way to get it to show on the basket or cart page?
 
AdBlock Detected

We get it, advertisements are annoying!

However in order to keep our huge array of resources free of charge we need to generate income from ads so to use the site you will need to turn off your adblocker.

If you'd like to have an ad free experience you can become a Babiato Lover by donating as little as $5 per month. Click on the Donate menu tab for more info.

I've Disabled AdBlock