Woocommerce:在注册时添加自定义消息



当有人第一次从同一页面注册时,我正在尝试使用 woocommerce 在"我的帐户"页面上添加一条消息 - 如果有人在支付订单时注册,我不想这样做。

我已经在过滤器和操作上弄乱了几个小时,而且我无法在注册后立即显示我的消息......我设法使用该功能wc_add_notice做的最好的事情是显示它,但在"我的帐户"页面的每个部分。

我不希望用户最终进入自定义页面,只需添加某种成功消息即可。

有人可以帮助我吗?我想自己做,而无需为这么简单的事情支付插件费用。

你在这里有相当多的工作。WooCommerce不区分在结账时注册的用户和通过我的帐户页面注册的用户。因此,您需要自己跟踪它,可能通过 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" />';
}

现在,您需要绑定到注册函数,以便可以访问此变量,并根据需要进行保存。

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
}

最后,如果设置了cookie,则可以在所需的页面上显示消息。您也可以取消设置 Cookie,以确保您不会再次显示它。

这可以通过操作或筛选器(如果是自定义函数或主题文件(来完成。

if( $_COOKIE['cookie_name'] ) {
//display message
//delete the cookie
}

可能有一个更简单的解决方案,但这将起作用...

最新更新