首页 文章

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

提问于
浏览
1

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

  • 2

    要在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


    仅在结帐页面中更简单的版本 for all users

    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

相关问题