douzhuanqian8244 2018-04-20 11:44 采纳率: 100%
浏览 71

更改错误通知文本并在Woocommerce中对此错误执行操作

I am not used to hooks in Wordpress and Woocommerce. I was wondering if it was possible to change an error notice text in Woocommerce using this dedicated filter hook:

add_filter('woocommerce_add_error', 'alter_check_for_afterpay', 100000);

function alter_check_for_afterpay($message) {

    //kijk of er in de foutmelding het woord afterpay in staat
    $find_afterpay = strpos($message, "AfterPay");

    if($find_afterpay == true){

         $message = "Helaas, is het niet gelukt om met AfterPay te betalen. U kunt uw order bestellen door ideal te gebruiken.";
         doSomeThing();
    }

    return $message;
}

function doSomeThing(){
    echo "Do something here";
}

I wanna do something when error is showing on the checkout page.

  • 写回答

1条回答 默认 最新

  • dongyi2083 2018-04-20 12:47
    关注

    Updated - You should try to use instead the following code, but this error notice need to be processed through wc_add_notice() or wc_print_notice() functions to be functional.

    Here is your revisited code:

    add_filter('woocommerce_add_error', 'alter_check_for_afterpay', 990 );
    function alter_check_for_afterpay( $message ) {
    
        if( strpos( $message, "AfterPay" ) !== false ){
    
             $message = __("Helaas, is het niet gelukt om met AfterPay te betalen. U kunt uw order bestellen door ideal te gebruiken.", "woocommerce");
        }
        return $message;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested (with another word instead of "AfterPay") and works. It could work for you.

    Your function doSomeThing() will not output anything using this filter hook, as it should need some specific action hook instead…

    If you want to do something (an action), You will need to target this error logic in dedicated woocommerce_after_checkout_validation action hook, this way:

    add_action('woocommerce_after_checkout_validation', 'alter_check_for_afterpay_action', 990, 2 );
    function alter_check_for_afterpay_action( $data, $errors ) {
        foreach ( $errors->get_error_messages() as $message ) {
            if( strpos($message, "AfterPay") !== false )
            {
                // ===> do your action here (but not echoing something !)
            }
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序