Изменение текста из стороннего плагина Woocommerce - PullRequest
2 голосов
/ 02 марта 2020

Я использую WooCommerce Wishlist и хочу изменить добавленный продукт на что-то другое. вот фрагмент:

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;
}

Как лучше всего менять этот текст из функции. php

Ответы [ 2 ]

2 голосов
/ 03 марта 2020

Я вижу, что вы уже приняли ответ, но вы спрашивали о том, чтобы включить его в функцию, так что вот как. Фильтр gettext принимает 3 параметра 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 );

Я лично ... Я бы скорее добавил несколько строк кода, чем целый плагин (который, вероятно, добавляет сценарии и css на мой сайт), чтобы сделать то, что может сделать этот маленький фрагмент.

2 голосов
/ 02 марта 2020

Поскольку строка уже включена в функцию __($string, $domain), вы можете легко перевести ее без использования кода или редактирования ваших функций. php. Все, что вам нужно, это плагин переводчика (я предлагаю использовать Loco Translate).

  • Установите плагин
  • После установки go в «Loco Translate»> «Плагины», используя левую меню столбцов на панели инструментов WP
  • Вы увидите список плагинов, установленных в настоящее время на вашем веб-сайте, выберите тот, который вы используете для управления списками WooCommerce
  • Затем выберите свой язык из списка или нажмите «Новый язык», если вашего языка нет (и вставьте языковые настройки, если вы создаете новый)
  • Выберите строку, которую вы хотите перевести, и поместите перевод в нижнюю текстовую область
  • Сохранить
...