skip to Main Content

I am trying to add product ID as a suffix in the product title for example:

iPhoneX – 123 (where 123 is the ID)

The reason why I need this is something related to the payment gateway I am dealing with. hence, this solution only needs to be applied where it is necessary for the payment gateway to recognize that ID during checkout.

The following code shows product ID as a suffix on checkout the way I wanted. however, When placing the order and redirecting to the third-party payment gateway page; the ID does not show there. I want the third-party payment gateway to display it as well.

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

function change_woocommerce_single_title($title, $id) {

if ( class_exists( 'woocommerce' ) && is_checkout())
  $title = $title . ' - ' . $id;
  
return $title;
}

2

Answers


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

    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;
    }
    
    Login or Signup to reply.
  2. Try this:

    // 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); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search