2015年11月17日 星期二

Paypal say "Data does not match input character set or default encoding" when WooCommerce Wordpress Checkout with Chinese words

Recently I am playing with Wordpress and WooCommerce shopping Cart plugin for wordpress to setup a simple E-commerce website, while I am using a plugin to add some extra info to my product ( WooCommerce Extra Fields ) with some chinese character, it randomly give me "Data does not match input character set or default encoding" during checkout.

After some digging on the network call, I found that the product name is in the Paypal link, and the chinese word is trimmed and become weird character.

I found the trim function wc_trim_string is the one to be blamed, and it is using php substr function.

The original wc_trim_string function in "woocommerce/include/wc-formatting-functions.php" contains the line:

 $string = substr( $string, 0, ( $chars - strlen( $suffix ) ) ) . $suffix;


i just replaced it with mb_substr and everything works like charm again. End result will look like:

 $string = mb_substr( $string, 0, ( $chars - strlen( $suffix ) ) ) . $suffix;


The reason is that substr function just trim without handling multiple-byte unicode character, and mb_substr is multi-byte safe.

This workaround should works on other multibyte/unicode character.

Note: Version of my WooCommerce is v2.4.8