【问题标题】:Avoiding error 500 when duplicating hooked functions in Woocommerce在 Woocommerce 中复制挂钩函数时避免错误 500
【发布时间】:2018-12-10 21:48:04
【问题描述】:

如果我在 functions.php 文件中使用 2 个代码。收到错误 500 !

使用以下代码作为按钮结帐页面自定义消息: Display a custom message for guest users in Woocommerce checkout page

并将以下代码用于顶部结帐页面自定义消息: Display a custom notice before all default notices in Woocommerce checkout page

如果我一起使用。页面消息的第一个(第一个链接)代码和顶部的两个代码(第二个链接)。不能一起工作。请帮帮我。

【问题讨论】:

  • 有什么办法可以将它们中的两个用于底部和顶部的两条不同的消息?
  • 每个函数都需要有一个唯一的名称...毕竟您可以更改钩子以满足您的需要(但wc_add_notice()用于template_redirect钩子和wc_print_notice()在其他钩子中),或将代码合并到一个独特的函数中(如我的回答中所添加)

标签: php wordpress function woocommerce hook-woocommerce


【解决方案1】:

如果您想要多个挂钩函数(是否重复),则每个函数都需要一个唯一的未使用名称:

add_action('template_redirect', 'my_custom_message_one');
function my_custom_message_one() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        wc_add_notice( __('This is my 1st custom message (for all)'), 'notice' );
    }
}

add_action('woocommerce_before_checkout_form', 'my_custom_message_two');
function my_custom_message_two() {
    if ( ! is_user_logged_in() ) {
        wc_print_notice( __('This is my custom message (for non logged users)'), 'notice' );
    }
}

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

或者你可以使用一个函数和两个通知合并在同一个函数中:

add_action('template_redirect', 'my_custom_message');
function my_custom_message() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        wc_add_notice( __('This is my 1st custom message (for all)'), 'notice' );
    }

    if ( ! is_user_logged_in() && is_checkout() && ! is_wc_endpoint_url() ) {
        wc_add_notice( __('This is my 2nd custom message (for non logged users)'), 'notice' );
    }
}

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

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2021-05-19
    • 2019-09-05
    • 2021-04-28
    • 2021-01-17
    相关资源
    最近更新 更多