2

if i use 2 codes in functions.php file. recive error 500 !

use below code for buttom checkout page custom message: Display a custom message for guest users in Woocommerce checkout page

and use below code for top checkout page custom message: Display a custom notice before all default notices in Woocommerce checkout page

if i use together. first (1st link) code for buttom page message and two code (2nd link) for top. cant work together. please help me.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
ahmadwp
  • 228
  • 1
  • 4
  • 15
  • Is there any way we can use two of them for two different messages in the bottom and top? – ahmadwp Jul 02 '18 at 16:15
  • 1
    Each function need to have a unique name… after all **you can change hooks** to feet your needs (but `wc_add_notice()` is used in `template_redirect` hook and `wc_print_notice()` in the other hooks), *or merge the code in one unique function (as added in my answer)*. – LoicTheAztec Jul 02 '18 at 16:35

1 Answers1

2

If you want multiple hooked functions (duplicated or not), each of them need a unique unused name:

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

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

enter image description here

Or you can use one function and 2 notices merged in the same function:

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

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

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399