php 更改“查看购物车”按钮的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18481472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:38:19  来源:igfitidea点击:

Change the text of 'View cart' button

phpwordpresswoocommerce

提问by Jhunmar Tomines

Iam using a woocommerce plugin but I got an problem on how to change the text of the view cart button hope there is a one who can help with my problem this is my sitethis is the image of the text that i want to change the imagehow can i edit it? all suggestion are appreciated.

我正在使用 woocommerce 插件,但我在如何更改查看购物车按钮的文本方面遇到了问题,希望有人可以帮助解决我的问题,这是我的网站,这是我想更改图像的文本图像我怎样才能编辑它?所有建议表示赞赏。

回答by Selom

Add the following to your functions.php file.

将以下内容添加到您的 functions.php 文件中。

/**
 * Change text strings
 *
 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
 */
function my_text_strings( $translated_text, $text, $domain ) {
    switch ( strtolower( $translated_text ) ) {
        case 'View Cart' :
            $translated_text = __( 'View Shopping Cart', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

This will change View Cart to View Shopping Cart

这会将查看购物车更改为查看购物车

回答by jodamo5

The example code put forward by Selom works, but is highly inefficient, as it forces all Wordpress text to be converted to lower case and then run through this filter to see if it matches a case. It has an immediate impact on load time as well as functions such as adding to cart. This is especially noticable if you are using ajax to add products to the cart without refreshing the page.

Selom 提出的示例代码有效,但效率非常低,因为它强制将所有 Wordpress 文本转换为小写,然后运行此过滤器以查看它是否与大小写匹配。它对加载时间以及添加到购物车等功能有直接影响。如果您使用 ajax 将产品添加到购物车而不刷新页面,这一点尤其明显。

It is far better to only run the text that is from the WooCommerce domain through a filter. This speeds things up a lot.

仅通过过滤器运行来自 WooCommerce 域的文本要好得多。这大大加快了速度。

I recommend using this example code instead, sourced from Rodolfo Melogli:

我建议改用这个示例代码,它来自 Rodolfo Melogli:

/**
 * @snippet       Translate a String in WooCommerce
 * @sourcecode    https://businessbloomer.com/?p=162
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate      https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 999, 3 );

function bbloomer_translate_woocommerce_strings( $translated, $text, $domain ) {


     if ( ! is_admin() && 'woocommerce' === $domain ) {

      switch ( strtolower( $translated ) ) {

         case 'view cart' :
            $translated = 'View/Edit Order';
            break;

         case 'proceed to checkout' :
            $translated = 'Place Order ';
            break;

          case 'checkout' :
            $translated = 'Place Order ';
            break;

    case 'add to cart' :
            $translated = 'Add to Order';
            break;


         // enter a new case for each line where you want Woocommerce text to be changed.

      }

   }   


    return $translated;
}

回答by Dnyanesh

Add the following code to your functions.php file

将以下代码添加到您的 functions.php 文件中

function change_view_cart_link( $params, $handle )
{
    switch ($handle) {
        case 'wc-add-to-cart':
            $params['i18n_view_cart'] = "Proceed to Cart"; //chnage Name of view cart button
            $params['cart_url'] = "http://myshop.com/custom-page"; //change URL of view cart button
        break;
    }
    return $params;
}

回答by Waiseman

Add the following snippet to your functions.php file

将以下代码段添加到您的 functions.php 文件中

function woo_custom_change_cart_string($translated_text, $text, $domain) {
    $translated_text = str_replace("view cart", "NEW TEXT HERE ", $translated_text);
    $translated_text = str_replace("View cart", "NEW TEXT HERE ", $translated_text);
return $translated_text;
}
add_filter('gettext', 'woo_custom_change_cart_string', 100, 3);
add_filter('ngettext', 'woo_custom_change_cart_string', 100, 3);

回答by victor

Go to wp-content\plugins\woocommerce\includes\wc-template-functions.phpthen edit raw 1429

转到wp-content\plugins\woocommerce\includes\wc-template-functions.php然后编辑原始 1429

function woocommerce_widget_shopping_cart_button_view_cart() {
        echo '<a href="' . esc_url( wc_get_cart_url() ) . '" class="button wc-forward">' . esc_html__( 'View cart', 'woocommerce' ) . '</a>';
    }
}
function woocommerce_widget_shopping_cart_button_view_cart() {
        echo '<a href="' . esc_url( wc_get_cart_url() ) . '" class="button wc-forward">' . esc_html__( 'your text', 'woocommerce' ) . '</a>';
    }
}

version woocommerce 3.0.3.

版本woocommerce 3.0.3