skip to Main Content

I need to replace the standard text "SKU:" with the "#" character, but my code doesn’t do it the way I want it to. It leaves the colon character ":" at the end

enter image description here

I wrote this code, but the colon character remains

function translate_woocommerce($translation, $text, $domain) {
    if ($domain == 'woocommerce') {
        switch ($text) {
            case 'SKU:':
                $translation = '#';
                break;
            case 'SKU':
                $translation = '#';
                break;
        }
    }
    return $translation;
}

add_filter('gettext', 'translate_woocommerce', 10, 3);

2

Answers


  1. I did something similar with chanting the word items to item.

    function change_woocommerce_item_text($translation, $single, $plural, $number, $context, $domain) { if ($domain == 'Divi' && ($translation == '%1$s Item' || $translation == '%1$s Items')) { return '%1$s'; } return $translation; }

    Take a look at this and see if this helps changing what you need.

    Login or Signup to reply.
  2. I did some fiddling around for you. Your code is almost correct, but it’s not replacing the "SKU:" text as you expect because the WooCommerce text domain may not always have the colon (":") character included in the text string. To ensure the replacement works regardless of whether the colon is present or not, you can modify the switch case to check for both "SKU:" and "SKU" separately.

    Here’s the modified code:

        function translate_woocommerce($translation, $text, $domain) {
        if ($domain == 'woocommerce') {
            switch ($text) {
                case 'SKU:':
                    $translation = '#';
                    break;
                case 'SKU':
                    $translation = '#';
                    break;
            }
        }
        return $translation;
    }
    
    add_filter('gettext', 'translate_woocommerce', 10, 3);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search