简体   繁体   English

在 Woocommerce 结帐页面中的所有默认通知之前显示自定义通知

[英]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我使用以下代码在结帐页面中向未登录的 woocommerce 用户(访问者)显示自定义消息

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:顶级代码接收这个论坛,来自@loictheaztec 先生我之前的问题链接如下:
Display a custom message for guest users in Woocommerce checkout page 在 Woocommerce 结帐页面中为来宾用户显示自定义消息

I would like to change woocommerce_before_checkout_form in code for move my message to top (first) in checkout page.我想在代码中更改woocommerce_before_checkout_form以将我的消息移动到结帐页面的顶部(第一个)。 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:要在 Woocommerce 登录消息之前和添加优惠券消息之前在结帐页面中显示您的自定义消息,您将使用以下代码:

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).代码位于活动子主题(或活动主题)的 function.php 文件中。 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).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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