简体   繁体   中英

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

我添加了我的问题的图片

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.

在此处输入图片说明


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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM