【问题标题】:Change error notice text and make an action for this error in Woocommerce在 Woocommerce 中更改错误通知文本并针对此错误采取措施
【发布时间】:2018-04-20 11:44:27
【问题描述】:

我不习惯在 Wordpress 和 Woocommerce 中使用钩子。我想知道是否可以使用此专用过滤器挂钩更改 Woocommerce 中的错误通知文本:

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";
}

当结帐页面上显示错误时,我想做点什么。

【问题讨论】:

  • 这不起作用@UnamataSanatarai:D

标签: php wordpress error-handling woocommerce hook-woocommerce


【解决方案1】:

已更新 - 您应该尝试使用以下代码代替,但此错误通知需要通过 wc_add_notice() 或 wc_print_notice() 函数处理才能正常工作。

这是您重新访问的代码:

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;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经测试(用另一个词代替“AfterPay”) 并且有效。它可能对你有用。

您的函数doSomeThing() 不会使用此过滤器挂钩输出任何内容,因为它应该需要一些特定的操作挂钩...

如果你想做某事(一个动作),你需要在专用的woocommerce_after_checkout_validation动作钩子中定位这个错误逻辑,这样:

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 !)
        }
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

【讨论】:

  • 但是,当过滤器的内容为 X 时,我如何提供操作?
  • @AdemAk 抱歉耽搁了,我正在测试... 我已经更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多