WooCommerce Notice消息,如何编辑它们?

问题描述 投票:14回答:5

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

wordpress woocommerce
5个回答
10
投票

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

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

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

所以在你的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;
}

11
投票

打开插件文件并搜索wc_add_notice

这个函数有一个过滤器:

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

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

使用这样的东西应该工作:

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

    return $message;
});

3
投票

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

这里有三个不同的文件,每个文件用于不同类型的通知。在我的情况下,我想在优惠券成功通知中添加一个类,所以我将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; ?>

2
投票

我遇到了这个答案,并且能够为生产站点实施。此答案与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;
 }

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


1
投票

我为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>
© www.soinside.com 2019 - 2024. All rights reserved.