• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Damien Carbery - Website Development

Getting You Online & Keeping You There

Getting You Online & Keeping You There Get Started
  • Home
  • Services
  • Maintenance Plans
  • Portfolio
  • Blog
  • About Me
  • Contact
You are here: Home / Website Development / Change hardcoded WooCommerce strings

Change hardcoded WooCommerce strings

Published: December 4, 2018

Change hardcoded WooCommerce strings
Use a PO and MO file to change some hardcoded WooCommerce strings. The same concept works for other plugins and themes.

WooCommerce provides a lot of actions, filters and templates to allow developers make extensive changes to WooCommerce online shops. Despite this, there are a few strings that are hardcoded in the core code. These strings are available for translation but what if you want to change them while keeping everything in English? (or your current active language)

How to ‘translate’ into English

WooCommerce provides a POT file (a template file for a PO file). This file has no translations in it (all the msgstr strings are empty). This is the starting point for our translation

I downloaded and installed the Poedit translations editor and opened the wp-content/plugins/woocommerce/i18n/languages/woocommerce.pot file in it. At the bottom of the screen I see this message:

Make new translation in Poedit
A POT file is not available to edit – it must be copied to a PO file and it will be edited with the translations. When I click ‘Create new translation‘ it prompts me for the translation language. As I’m not changing the language I must determine what locale (aka ‘language‘) WordPress is running as.

When I run this it reports Locale: en_US aka English – United States. The ‘en_US‘ will be used in the filename of the .MO file that I will create (it’s not a requirement but it’s good for organisation).

Choose translation language of English United States in Poedit
I chose “English (United States)” and began my changes.

Change some strings and create the MO file

I went through the WooCommerce front end, viewing products, adding them to the cart and proceeding to checkout. Every string I examined could be changed via a filter or template file. The WooCommerce team have done an excellent job.

When I removed an item from the cart I found strings that cannot be changed with a filter or template file.
WooCommerce message when item removed from cart
This message is made up of 2 strings: ‘%s removed‘ and ‘Undo?‘ from includes/class-wc-form-handler.php.

I changed these to be a little more personal:
Translate two strings

I then compiled them into a MO file with Poedit. The MO file is quite small – only 601 bytes as it only contains the changed strings.

Where to put the MO file

Next is to figure out where to put the MO file so that WooCommerce will load it.
I searched the WooCommerce source for load_textdomain() and found it in includes/class-woocommerce.php. This function is defined in wp-includes/l10n.php and it has the ‘load_textdomain_mofile‘ filter to allow changing the path of the MO file that is loaded.

I renamed my MO file to woocommerce-en_US.mo. This is primarily to help me identify the file’s purpose and locale.

The load_textdomain() function is called multiple times. In my test installation it was called 22 times on one page load – called by WordPress, the theme, WooCommerce and the Query Monitor plugins.

Putting the code into a class allowed me set a member variable when the ‘load_textdomain_mofile‘ filter was called and the custom MO file loaded. I check this variable in the ‘override_load_textdomain‘ filter function and can return ‘true‘ to override the .mo file loading.

I wrote the following code to simply set the WooCommerce MO file to the woocommerce-en_US.mo file that I copied to the same directory as the plugin file.

Now when I remove an item from the cart I see the changed strings.
New text when an item is removed from the cart

Note

This concept of changing a few strings with a custom MO file can be applied to other plugins (or themes) that do not make it as easy to change them as WooCommerce does.

Reader Interactions

Comments

  1. Kevin says

    January 26, 2021 at 1:52 am

    This is a fantastic solution. Thanks man!

    Reply

Leave a Reply Cancel reply

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


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Blog Categories

  • Being Green
  • Food
  • Grammar
  • Just Strange
  • Laziness
  • Personal
  • Portfolio
  • Race Reports
  • Shudder
  • Typo
  • Website Development
  • WooCommerce Tips
  • Facebook
  • Twitter

Copyright © 2013–2023 Damien Carbery · Using Genesis Framework · Privacy Policy