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

时间:2018-07-02 14:38:29

标签: php wordpress woocommerce checkout hook-woocommerce

我使用以下代码在结帐页面中向未登录的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' );
    }
}

顶级代码重新回到这个论坛,来自@loictheaztec先生 我之前的问题链接如下:
Display a custom message for guest users in Woocommerce checkout page

我想更改代码中的 woocommerce_before_checkout_form ,以便将我的消息移至结帐页面的顶部(第一)。但是我不知道该怎么做。 我只知道(与结帐页面有关)下面的那两个钩子:

  • woocommerce_before_checkout_form
  • woocommerce_after_checkout_form

i added picture of my problem

1 个答案:

答案 0 :(得分:3)

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

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

enter image description here


仅在结帐页面中针对所有用户的更简单版本

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

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

enter image description here