更改 woocommerce 错误消息

问题描述 投票:0回答:2

我想更改当客户未填写所有必填字段时结帐页面上出现的错误消息的文本。目前,该消息将显示需要填写的每个字段,例如: “帐单地址是必填字段。 帐单邮政编码是 q 必填字段。 电子邮件地址是必填字段。 ...”

我只想要一条消息“请填写必填字段”。或者,如果需要填写多个字段,请填写“请填写必填字段。”

有人知道如何染色吗?我的 PHP 能力有限。 谢谢!

php wordpress woocommerce
2个回答
3
投票

更新代码:

在 function.php 文件中添加此代码:

function my_woocommerce_add_error( $error ) {
    if (strpos($error,'required') !== false) {
        $error = 'Required';
    }
    return $error;
}
add_filter( 'woocommerce_add_error', 'my_woocommerce_add_error' );

并将此代码添加到notices rror.php文件中:

<ul class="woocommerce-error">
    <?php 
    $totalReq = count(array_keys($messages, 'Required')); 
    $removeReq = array_diff($messages, ["Required"]);

    if($totalReq > 1){
        echo '<li>Please fill in all the <strong>required</strong> fields.</li>';
    }else{
        echo '<li>Please fill in the <strong>required</strong> field.</li>';
    }
    ?>
    <?php foreach ( $removeReq as $message ) : ?>
        <li><?php echo wp_kses_post( $message ); ?></li>
    <?php endforeach; ?>
</ul>

希望能解决您的疑虑:)


0
投票

此代码对于一般所需的错误消息显示效果很好。但在 strpos 函数中搜索“required”字符串也取决于网站的语言,需要根据您的网站语言进行更改。

另外,错误模板文件的代码需要更改为

$removeReq = array_diff($messages, ["Required"]);

$removeReq = array_diff($messages, array("Required"));

它就像一个魅力。 覆盖错误消息的好主意。尚未找到解决方案,但这确实很好。

但它适用于所有 woocommerce 表单,例如从我的帐户页面编辑地址等..

© www.soinside.com 2019 - 2024. All rights reserved.