Add Product ID to Product title Woocommerce [HELP]

Saint Gabriel

Well-known member
Jan 3, 2020
2,998
3,049
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.
 

Saint Gabriel

Well-known member
Jan 3, 2020
2,998
3,049
113
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.
 

Proxybunker

Well-known member
Jan 3, 2021
2,660
1,841
120
codecanyonscripts.com
@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

Well-known member
Jan 3, 2020
2,998
3,049
113
@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

Proxybunker

Well-known member
Jan 3, 2021
2,660
1,841
120
codecanyonscripts.com
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

Saint Gabriel

Well-known member
Jan 3, 2020
2,998
3,049
113
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.
 

TylerD

Member
Oct 28, 2020
93
35
18
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:

Saint Gabriel

Well-known member
Jan 3, 2020
2,998
3,049
113
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?
 

TylerD

Member
Oct 28, 2020
93
35
18
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!
 

Saint Gabriel

Well-known member
Jan 3, 2020
2,998
3,049
113
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.
 

Saint Gabriel

Well-known member
Jan 3, 2020
2,998
3,049
113
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?
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu