skip to Main Content

I’m using WooCommerce Wishlist and want to change product added to something else.
here is the snippet:

public static function add_to_wishlist_button( $url, $product_type, $exists ) {
    _deprecated_function( 'add_to_wishlist_button', '2.0.0', 'add-to-wishlist-button.php template' );
    global $yith_wcwl, $product;
    $product_id = yit_get_product_id( $product );
    $label_option = get_option( 'yith_wcwl_add_to_wishlist_text' );
    $localize_label = function_exists( 'icl_translate' ) ? icl_translate( 'Plugins', 'plugin_yit_wishlist_button', $label_option ) : $label_option;
    $label = apply_filters( 'yith_wcwl_button_label', $localize_label );
    $icon = get_option( 'yith_wcwl_add_to_wishlist_icon' ) != 'none' ? '<i class="fa ' . get_option( 'yith_wcwl_add_to_wishlist_icon' ) . '"></i>' : '';
    $classes = get_option( 'yith_wcwl_use_button' ) == 'yes' ? 'class="add_to_wishlist single_add_to_wishlist button alt"' : 'class="add_to_wishlist"';
    $html  = '<div class="yith-wcwl-add-to-wishlist">';
    $html .= '<div class="yith-wcwl-add-button';  // the class attribute is closed in the next row
    $html .= $exists ? ' hide" style="display:none;"' : ' show"';
    $html .= '><a href="' . esc_url( add_query_arg( 'add_to_wishlist', $product_id ) ) . '" data-product-id="' . $product_id . '" data-product-type="' . $product_type . '" ' . $classes . ' >' . $icon . $label . '</a>';
    $html .= '<img src="' . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" class="ajax-loading" alt="loading" width="16" height="16" style="visibility:hidden" />';
    $html .= '</div>';
    $html .= '<div class="yith-wcwl-wishlistaddedbrowse hide" style="display:none;"><span class="feedback">' . __( 'Product added!','yith-woocommerce-wishlist' ) . '</span> <a href="' . esc_url( $url ) . '">' . apply_filters( 'yith-wcwl-browse-wishlist-label', __( 'Browse Wishlist', 'yith-woocommerce-wishlist' ) ) . '</a></div>';
    $html .= '<div class="yith-wcwl-wishlistexistsbrowse ' . ( $exists ? 'show' : 'hide' ) . '" style="display:' . ( $exists ? 'block' : 'none' ) . '"><span class="feedback">' . __( 'The product is already in the wishlist!', 'yith-woocommerce-wishlist' ) . '</span> <a href="' . esc_url( $url ) . '">' . apply_filters( 'yith-wcwl-browse-wishlist-label', __( 'Browse Wishlist', 'yith-woocommerce-wishlist' ) ) . '</a></div>';
    $html .= '<div style="clear:both"></div><div class="yith-wcwl-wishlistaddresponse"></div>';
    $html .= '</div>';
    $html .= '<div class="clear"></div>';
    return $html;
}

What is the best practice to change that text from function.php

2

Answers


  1. Since the string is already wrapped into the function __($string, $domain), you can easily translate it without using any code or editing your functions.php.
    All you need is a translator plugin (I suggest using Loco Translate).

    • Install the plugin
    • After installation go to “Loco Translate” > “Plugins” using the left column menu on WP dashboard
    • You will see a list of plugins currently installed on your website, select the one you use to manage WooCommerce Whislists
    • Then select your language from the list, or click on “New language” if your language isn’t there (and insert the language settings if you are creating a new one)
    • Select the string you want to translate, and put the translation in the bottom text-area
    • Save
    Login or Signup to reply.
  2. I see you’ve already accepted an answer, but you asked about putting this into a function, so here’s how. Filter gettext accepts 3 parameters gettext Hook

    // Change text strings and make sure you're only on the specic text domain
    function dd_change_text_strings( $translated_text, $text, $domain ) {
        switch ( $translated_text ) {
            case 'Product added!' :
                // Make sure you're only using the phrase in the specific text domain you want.
                if ($domain == 'yith-woocommerce-wishlist'){
                    $translated_text = __( 'Something Else','your_text_domain');
                } else {
                    // otherwise use the default text, in case Product addded! is used somewhere else in another text domain 
                    // This is optional
                    $translated_text = __( 'Product added!', $domain);
                }
                break;
        }
        return $translated_text;
    }
    add_filter( 'gettext', 'dd_change_text_strings', 20, 3 );
    

    Me personally… I would rather add a few lines of code than have an entire plugin (which probably adds scripts and css overhead to my site) to do what this little snippet can do.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search