4

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
ahmadwp
  • 228
  • 1
  • 4
  • 15
  • I think what you need is to do template override. Override the default of woocommerce and create your own, which offers more functionality. – rai nalasa Jul 02 '18 at 14:42

1 Answers1

9

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thank you. work correctly. if i convert this code for show my custom message to all website users (logged and unlogged) What should I do? – ahmadwp Jul 02 '18 at 15:47
  • 1
    You should just remove `! is_user_logged_in() && ` from the if statement... – LoicTheAztec Jul 02 '18 at 15:50
  • 1
    +1 - you've really been crushing it on SO! Nice! As a note - I've _stopped_ recommending putting functions like this in the theme's `functions.php` file, and instead have started recommending creating a small plugin that contains the "special requirements" of a given site, to ensure a site would survive a theme update or change. – random_user_name Jul 02 '18 at 15:50
  • 2
    @cale_b … Thanks. **Note:** There is multiple ways when customizing Woocommerce. But **always use a child theme** *(usefull to override woocommerce templates and some other themes files)* and it survives to the updates. Now **plugins are more difficult to handle**, so using function.php file in a child theme is just fine for most users. – LoicTheAztec Jul 02 '18 at 15:55
  • @ahmadsh I have added a simpler code notice for all users in checkout page at the end of my answer… It will work. – LoicTheAztec Jul 02 '18 at 16:07