我目前正在创建一个小部件,以便在使用WooCommerce的WordPress网站上显示注册表单。目前,我只有3个基本字段,分别是电子邮件,密码,重复密码。我期待添加更多WooCommerce字段,但想在跳到下一步之前解决这个问题。
我的邮件输出有问题(密码错误,帐户已存在等)。
我在网上搜索,并且在注册页面旁边没有为WooCommerce注册构建的短代码。所以我继续创建了一个带有模板部分的短代码。
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
这是我用来在变量中获取模板部分的片段,因为默认函数会“回显”内容而不是“返回”它。
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
所以,问题是,当我从模板部分调用woocommerce_show_messages
或$woocommerce->show_messages();
时,没有显示任何内容,或者如果是,则显示在页面顶部。
我确实尝试将调用放在我的短代码函数中:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
这样做,<head>
标签内的消息输出,这不是我想要的。
我尝试使用ob_start()
,ob_get_contents()
和ob_clean()
执行相同的操作,但没有任何显示。该变量将为空。
我也尝试将woocommerce_show_messages
挂钩到核心中看到的动作:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
类似于:
add_action( 'before_registration_form', 'woocommerce_show_messages');
我在模板部分添加了这个:
<?php do_action('before_registration_form'); ?>
但我仍然无法设法在框内显示错误消息。它将始终插入<head>
当一切都完成后,我将分享最终解决方案。
感谢您的时间,
于连
答案 0 :(得分:2)
我终于通过将自定义函数挂钩到我的header.php
我想在模板部分中挂钩函数不能按预期工作。
在header.php中,我得到了这个:
do_action('theme_after_header');
这是钩住的功能。效果很好。
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
但是,我会调查'解除'原始节目消息功能,因为它可能会显示两次。需要再测试一些;)
答案 1 :(得分:2)
您也可以在模板中使用[woocommerce_messages]短代码来显示
答案 2 :(得分:0)
回答一些旧问题,但您也可以尝试以下方法:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}