Skip to content
Advertisement

Display a custom notice before all default notices in Woocommerce checkout page

I use the following below code for show custom message to un-logged woocommerce users (visitors) in checkout page

add_action('woocommerce_before_checkout_form', 'my_custom_message');
function my_custom_message() {
if ( ! is_user_logged_in() ) {
       wc_print_notice( __('This is my custom message'), 'notice' );
    }
}

top code recive this forum, from mr @loictheaztec my before question link below:
Display a custom message for guest users in Woocommerce checkout page

I would like to change woocommerce_before_checkout_form in code for move my message to top (first) in checkout page. But I have no idea how to do it. I only know those two hooks below (related to checkout page):

  • woocommerce_before_checkout_form
  • woocommerce_after_checkout_form

i added picture of my problem

Advertisement

Answer

To display Your custom message in checkout page before the Woocommerce login message and before add coupon message, you will use the following code instead:

add_action('template_redirect', 'my_custom_message');
function my_custom_message() {
    if ( ! is_user_logged_in() && is_checkout() && ! is_wc_endpoint_url() ) {
        wc_add_notice( sprintf( __('This is my <strong>"custom message"</strong> and I can even add a button to the right… <a href="%s" class="button alt">My account</a>'), get_permalink( get_option('woocommerce_myaccount_page_id') ) ), 'notice' );
    }
}

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

enter image description here


A more simpler version for all users in checkout page only:

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

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

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement