WooCommerce – Change “Related Products” text

This snippet allows to quickly change the “Related products” text which is shown by default at the bottom of a single product page in a WooCommerce shop.

Check out on my test environment: https://shop.nicolasrabier.com/product/woo-album-4/

Insert the code in functions.php of your child theme.

/**
* Change text strings
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
*/
function custom_related_products_text( $translated_text, $text, $domain ) {
  switch ( $translated_text ) {
    case 'Related products' :
      $translated_text = __( 'Other items you might like', 'woocommerce' );
      break;
  }
  return $translated_text;
}
add_filter( 'gettext', 'custom_related_products_text', 20, 3 );

Post context:

  • WordPress
  • WooCommerce

Photo by Fikret tozak on Unsplash

12 comments

    1. Sorry for the late reply.
      Wordpress best practices encourage to create a child theme when personalizing your theme as it allows you to keep receiving theme updates without losing your custom changes.

  1. Could we change the text based on the product category?
    I have implemented a hook for that on archive pages and products loop, but couldn’t manage to do it with related products

    Thanks

  2. hello sir

    is that possible to wrap this function in “plugin”

    I mean translate string via get option ?

    here is my plugin code which i try to achieve
    ====================================================

    woocommerce_related_products_text_changer_options = get_option( ‘woocommerce_related_products_text_changer_option_name’ ); ?>

    WooCommerce \”Related Products\” Text Changer
    WooCommerce \”Related Products\” Text Changer plugin settings

    <?php }

    public function woocommerce_related_products_text_changer_page_init() {
    register_setting(
    'woocommerce_related_products_text_changer_option_group', // option_group
    'woocommerce_related_products_text_changer_option_name', // option_name
    array( $this, 'woocommerce_related_products_text_changer_sanitize' ) // sanitize_callback
    );

    add_settings_section(
    'woocommerce_related_products_text_changer_setting_section', // id
    'Settings', // title
    array( $this, 'woocommerce_related_products_text_changer_section_info' ), // callback
    'woocommerce-related-products-text-changer-admin' // page
    );

    add_settings_field(
    'enter_your_text_here_0', // id
    'Enter Your Text Here', // title
    array( $this, 'enter_your_text_here_0_callback' ), // callback
    'woocommerce-related-products-text-changer-admin', // page
    'woocommerce_related_products_text_changer_setting_section' // section
    );
    }

    public function woocommerce_related_products_text_changer_sanitize($input) {
    $sanitary_values = array();
    if ( isset( $input['enter_your_text_here_0'] ) ) {
    $sanitary_values['enter_your_text_here_0'] = sanitize_text_field( $input['enter_your_text_here_0'] );
    }

    return $sanitary_values;
    }

    public function woocommerce_related_products_text_changer_section_info() {

    }

    public function enter_your_text_here_0_callback() {
    printf(
    '’,
    isset( $this->woocommerce_related_products_text_changer_options[‘enter_your_text_here_0’] ) ? esc_attr( $this->woocommerce_related_products_text_changer_options[‘enter_your_text_here_0’]) : ”
    );
    }

    }
    if ( is_admin() )
    $woocommerce_related_products_text_changer = new WooCommerceRelatedProductsTextChanger();

    /*
    * Retrieve this value with:
    * $woocommerce_related_products_text_changer_options = get_option( ‘woocommerce_related_products_text_changer_option_name’ ); // Array of All Options
    * $enter_your_text_here_0 = $woocommerce_related_products_text_changer_options[‘enter_your_text_here_0’]; // Enter Your Text Here
    */
    /**
    below is your code
    */
    /**
    * Change text strings
    *
    * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
    */

    function custom_related_products_text( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
    case ‘Related products’ :
    $translated_text = __( ‘Other items you might like’, ‘woocommerce’ );
    break;
    }
    return $translated_text;
    }
    add_filter( ‘gettext’, ‘custom_related_products_text’, 20, 3 );

  3. Awesome man! Running a non-profit website with Woocommerce and they were uncomfortable with the word “product” for their donation levels, which I had to agree with. This worked like a charm!

Leave a Reply

Your email address will not be published. Required fields are marked *