[英]Woocommerce: add a custom message on registration
I'm trying to add a message on the "my account" page with woocommerce when somebody registers for the first time from the same page - I don't want to do it if somebody registers while paying for an order. 当有人从同一页面首次注册时,我正在尝试在woocommerce的“我的帐户”页面上添加一条消息-如果有人在付款时进行注册,我不想这样做。
I've been messing around for hours with filters and actions, and I can't manage to display my message just after the registration... The best thing I've managed to do with the function wc_add_notice is to display it but on every single part of "my account" page. 我已经花了好几个小时来使用过滤器和操作了,在注册后我无法设法显示我的消息……我设法用wc_add_notice函数做的最好的事情是显示它,但是在每一个“我的帐户”页面的一部分。
I don't want the user to end up on a custom page, just add some kind of success message. 我不希望用户最终进入自定义页面,只需添加某种成功消息即可。
Could someone help me with that? 有人可以帮我吗? I'd like to do it myself, without paying for a plugin for such a simple thing. 我想自己做,而不必为这样简单的事情支付插件费用。
You'd have quite a bit of work here. 您将在这里完成很多工作。 WooCommerce does not distinguish between a user registering during checkout versus a user registering via the my account page. WooCommerce不会区分结帐时注册的用户和通过我的帐户页面注册的用户。 So, you would need to track this yourself, possibly via a POST variable. 因此,您可能需要自己通过POST变量进行跟踪。
add_action('woocommerce_register_form_end', 'add_hidden_field_to_register_form');
function add_hidden_field_to_register_form() {
//we only want to affect the my account page
if( ! is_account_page() )
return;
//alternatively, try is_page(), or check to see if this is the register form
//output a hidden input field
echo '<input type="hidden" name="non_checkout_registration" value="true" />';
}
Now, you will need to tie into the registration function so you can access this variable, and save as needed. 现在,您将需要使用注册功能,以便可以访问此变量,并根据需要进行保存。
add_action( 'woocommerce_created_customer', 'check_for_non_checkout_registrations', 10, 3 );
function check_for_non_checkout_registrations( $customer_id, $new_customer_data, $password_generated ) {
//ensure our custom field exists
if( ! isset( $_POST['non_checkout_registration'] ) || $_POST['non_checkout_registration'] != 'true' )
return;
//the field exists. Do something.
//since I assume it will redirect to a new page, you need to save this somehow, via the database, cookie, etc.
//set a cookie to note that this user registered without a checkout session
setcookie( ... );
//done
}
Finally, you can display the message on the page desired, if the cookie is set. 最后,如果已设置cookie,则可以在所需页面上显示消息。 You can unset the cookie as well, to ensure you do not show it again. 您也可以取消设置cookie,以确保不再显示它。
This could be done via an action or filter, if a custom function, or a theme file. 这可以通过操作或过滤器(如果是自定义功能)或主题文件来完成。
if( $_COOKIE['cookie_name'] ) {
//display message
//delete the cookie
}
There may be a simpler solution, but this would work... 可能有一个更简单的解决方案,但这将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.