WooCommerce: Append Text To Product Titles Tutorial - Robert Mullineux

WooCommerce: Append Text To Product Titles

Unleash the power of customisation: Learn how to append text to your WooCommerce product titles

Facebook
Twitter
Reddit

Welcome to this article on how to append text to product titles in WooCommerce. In this tutorial, I’ll be walking you through the process of adding a specific string of text to the end of all of your product titles in your WooCommerce store. This can be a useful technique for distinguishing different types of products or for adding important information to the title, such as the product’s SKU or a sale price.

I’ll be showing you how to accomplish this using simple code snippets and the functions.php file of your WordPress theme. This is a relatively straightforward process that can save you a lot of time and effort when it comes to managing your product titles in WooCommerce. So, Let’s get started!

Six Handy Snippets To Help Append Text To Product Titles

Append Text To Product Titles (Single Product Page)

Here’s an example of a function that you can use to append the text “YOUR TEXT” to the product title on the single product template in WooCommerce:


function append_text_to_product_title() {
    global $product;
    $product_title = $product->get_name();
    $product->set_name( $product_title . ' YOUR TEXT');
}
add_action( 'woocommerce_single_product_summary', 'append_text_to_product_title', 5 );

This function uses the woocommerce_single_product_summary action hook to target the product title on the single product template. Inside the function, the global $product variable is used to access the product object and retrieve its title using the get_name() method. The text “YOUR TEXT” is then appended to the end of the title using the concatenation operator (.) . After that we set the new title by using set_name( $product_title . ‘ YOUR TEXT’) method.

It’s important to note that this code snippet will append the text to the product title on all single product pages, if you want to apply this function to specific products, you can use conditional statements to check product id or category etc before applying the function.

Append Text To Product Titles – If Product In Specific Category (Single Product Page)

You can also set specific conditions surrounding the visibility of the appended text.

Here’s an example of a function that you can use to append the text “YOUR TEXT” to the title of a product that belongs to the product category “Upcoming Products” on the single product template in WooCommerce:


function append_text_to_upcoming_product_title() {
    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        foreach ( $terms as $term ) {
            if ( $term->slug === 'upcoming-products' ) {
                global $product;
                $product_title = $product->get_name();
                $product->set_name( $product_title . ' YOUR TEXT');
            }
        }
    }
}
add_action( 'woocommerce_single_product_summary', 'append_text_to_upcoming_product_title', 5 );

This function uses the woocommerce_single_product_summary action hook to target the product title on the single product template. First, it uses the global $post variable to get the product id, and then it uses the get_the_terms() function to retrieve all of the categories that the product belongs to. Next, It checks if the product belongs to the category ‘upcoming-products’ by checking if the slug of the category is ‘upcoming-products’. If it is a match we then set the new title like in previous example.

Append Text To Product Titles (Cart Page Table)

Here’s an example of a function that you can use to append the text “YOUR TEXT” to the product title in the cart table on the cart page in WooCommerce:


function append_text_to_cart_product_title( $product_name, $cart_item, $cart_item_key ) {
    $product_name .= ' YOUR TEXT';
    return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'append_text_to_cart_product_title', 10, 3 );

This function uses the woocommerce_cart_item_name filter hook to target the product title in the cart table on the cart page. The function takes three arguments: $product_name, $cart_item, and $cart_item_key. Here, we are appending the text “YOUR TEXT” to the product name by using the concatenation operator (.). After that we return the modified product name.

Append Text To Product Titles – If Product In Specific Category (Cart Page Table)

Here’s an example of a function that you can use to append the text “YOUR TEXT” to the title of a product that belongs to the product category “Upcoming Products” on the cart page table in WooCommerce:


function append_text_to_upcoming_product_title_cart( $product_name, $cart_item, $cart_item_key ) {
    $product_id = $cart_item['product_id'];
    $terms = get_the_terms( $product_id, 'product_cat' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        foreach ( $terms as $term ) {
            if ( $term->slug === 'upcoming-products' ) {
                $product_name .= ' YOUR TEXT';
                break;
            }
        }
    }
    return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'append_text_to_upcoming_product_title_cart', 10, 3 );

This function uses the woocommerce_cart_item_name filter hook to target the product title on the cart page. This function takes three arguments: $product_name, $cart_item, and $cart_item_key. First, it gets the product id using $cart_item[‘product_id’] and then it uses the get_the_terms() function to retrieve all of the categories that the product belongs to. Next, It checks if the product belongs to the category ‘upcoming-products’ by checking if the slug of the category is ‘upcoming-products’. If it is a match we then append the text to the product name.

Append Text To Product Titles (Checkout Page Order Review Table)

Here’s an example of a function that you can use to append the text “YOUR TEXT” to the product title on the checkout page order review table in WooCommerce:


function append_text_to_product_title_checkout( $product_name, $values, $cart_item_key ) {
    $product_name .= ' YOUR TEXT';
    return $product_name;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'append_text_to_product_title_checkout', 10, 3 );

Append Text To Product Titles – If Product In Specific Category (Checkout Page Order Review Table)

Here’s an example of a function that you can use to append the text “YOUR TEXT” to the title of a product that belongs to the product category “Upcoming Products” on the checkout page order review table in WooCommerce:


function append_text_to_upcoming_product_title_checkout($product_name, $values, $cart_item_key ) {
    $product_id = $values['product_id'];
    $terms = get_the_terms( $product_id, 'product_cat' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        foreach ( $terms as $term ) {
            if ( $term->slug === 'upcoming-products' ) {
                $product_name .= ' YOUR TEXT';
                break;
            }
        }
    }
    return $product_name;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'append_text_to_upcoming_product_title_checkout', 10, 3 );

You can add any of these code snippets to your theme’s functions.php file, or if you are using a child theme, you can add it to the child theme’s functions.php file.

If you’re interested in customising your WooCommerce store even further, the official WooCommerce developer resources website is a great place to start. This website provides a wealth of information on hooks, filters, and functions that can be used to customise WooCommerce.

If you’ve read this article and you’re still feeling unsure about how to append text to product titles, or you want to take your eCommerce store to the next level, don’t hesitate to reach out for help. I am an experienced eCommerce developer and I’d be happy to assist you with your development needs. Whether you need a custom plugin, custom designed website, or just some advice on how to customise your store, I can provide you with the expertise and support you need to take your business to the next level. Contact me today and let’s work together to achieve your goals.

Facebook
Twitter
Reddit

Get In Touch