php - 在 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 先生,我之前的问题链接如下:
在 Woocommerce 结帐页面中为来宾用户显示自定义消息
我想在代码中更改woocommerce_before_checkout_form以将我的消息移动到结帐页面的顶部(第一个)。但我不知道该怎么做。我只知道下面的两个钩子(与结帐页面有关):
woocommerce_before_checkout_form
woocommerce_after_checkout_form
解决方案
要在 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 文件中。测试和工作。
仅适用于结帐页面中的所有用户的更简单版本:
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 文件中。测试和工作。