wordpress WooCommerce 通知消息,我如何编辑它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26188946/
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-09-08 11:01:29  来源:igfitidea点击:

WooCommerce Notice Messages, how do I edit them?

wordpresswoocommerce

提问by Majo0od

I'm trying to figure out where WooCommerce creates it's messages for when there is a success, error or notice in WooCommerce. I want to edit those messages to fit the scenario more neatly and also edit the HTML. Where are these messages located and how do I edit them?

我试图找出 WooCommerce 在 WooCommerce 中出现成功、错误或通知时在哪里创建它的消息。我想编辑这些消息以更巧妙地适应场景并编辑 HTML。这些消息位于何处以及如何编辑它们?

采纳答案by Yavor

Many of them are directly in the plugin files - unfortunately. Some messages are tied to filter hooks that allow you to edit them without messing with plugin files but that's not always the case.

其中许多直接在插件文件中 - 不幸的是。某些消息与过滤器挂钩相关联,允许您编辑它们而不会弄乱插件文件,但情况并非总是如此。

The message you wanted to change was "Product Name was successfully added to your cart". This one is set in the function wc_add_to_cart_message in wc-cart-functions.php and this function allows you to change it using a filter:

您想要更改的消息是“产品名称已成功添加到您的购物车”。这是在 wc-cart-functions.php 的 wc_add_to_cart_message 函数中设置的,该函数允许您使用过滤器更改它:

wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );

So in your functions.php file you could add something like:

所以在你的functions.php文件中你可以添加如下内容:

add_filter('wc_add_to_cart_message', 'handler_function_name', 10, 2);
function handler_function_name($message, $product_id) {
    return "Thank you for adding product" . $product_id;
}

回答by brasofilo

Open the plugin files and search for wc_add_notice:

打开插件文件并搜索wc_add_notice

This function has a filter:

这个函数有一个过滤器:

apply_filters( 'woocommerce_add_' . $notice_type, $message );

The $notice_typeis the second argument passed in all those occurrences.

$notice_type是在所有这些事件中传递的第二个参数。

Using something like this should work:

使用这样的东西应该有效:

add_filter( 'woocommerce_add_error', function( $message ) {
    if( $message == 'Some message' )
        $message = '';

    return $message;
});

回答by Ollie Murphy

The filters mentioned here work fine for editing the message itself, but if you want to edit the actual HTML markup containing the notice message, then you need to use the notice templates under templates > notices.

此处提到的过滤器可以很好地编辑消息本身,但如果要编辑包含通知消息的实际 HTML 标记,则需要使用templates > notices.

There are three different files here, each for the different kinds of notices. In my case, I wanted to add a class to the coupon applied successfully notice, so I copied success.phpover into my theme file. My code then looked like below:

这里有三个不同的文件,每个文件用于不同类型的通知。就我而言,我想在优惠券应用成功通知中添加一个类,所以我复制success.php到我的主题文件中。我的代码如下所示:

<?php foreach ( $messages as $message ) : ?>
    <?php 
        $om_css_class = "";
        if ( $message == "Coupon code applied successfully." ) {
            $om_css_class = "coupon-notice-msg";
        } 
    ?>
    <div class="woocommerce-message <?php echo $om_css_class; ?>"><?php echo wp_kses_post( $message ); ?></div>
<?php endforeach; ?>

回答by user3795286

I came across this answer and was able to implement for a production site. This answer is related to woocommerce error codes notices. You need to find the codes in the separate class files (~woocommerce/includes/). For my purpose the code was in ~woocommerce/includes/class-wc-coupon.php

我遇到了这个答案,并且能够为生产站点实施。此答案与 woocommerce 错误代码通知有关。您需要在单独的类文件 (~woocommerce/includes/) 中找到代码。为了我的目的,代码在 ~woocommerce/includes/class-wc-coupon.php

/**
 * Modify the coupon errors:
*/

 add_filter( 'woocommerce_coupon_error', 'wpq_coupon_error', 10, 2 );

 function wpq_coupon_error( $err, $err_code ) {
  return ( '103' == $err_code ) ? '' : $err;
 }

Thanks to this page: http://wpquestions.com/WooCommerce_Remove_Coupon_code_already_applied_error_message/10598

感谢此页面:http: //wpquestions.com/WooCommerce_Remove_Coupon_code_already_applied_error_message/10598

回答by developer

I did it for error.phpfile. file path is woocommerce/templates/notices/error.php

我是为了error.php文件做的。文件路径是 woocommerce/templates/notices/error.php

<ul class="woocommerce-error" role="alert">
    <?php
  foreach ( $messages as $message ) :

  if($message=="<strong>Billing Email address</strong> is a required field.") { $message="<strong>Email address</strong> is a required field."; }?>
        <li><?php echo wp_kses_post( $message ); ?></li>
    <?php endforeach; ?>
</ul>