在Woocommerce中复制钩子函数时避免错误500

时间:2018-07-02 16:06:24

标签: php wordpress function woocommerce hook-woocommerce

如果我在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

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

1 个答案:

答案 0 :(得分:2)

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

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文件中。经过测试,可以正常工作。

enter image description here

或者您可以使用一个功能,并在同一功能中合并2个通知:

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文件中。经过测试,可以正常工作。

enter image description here