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

问题描述 投票:0回答:1

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

我一直在使用过滤器和操作乱搞几个小时,而且我无法在注册后立即显示我的消息...我用wc_add_notice函数设法做的最好的事情是显示它但是每一个“我的帐户”页面的单个部分。

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

有人可以帮助我吗?我想自己做,不用为这么简单的东西买插件。

php wordpress woocommerce registration
1个回答
0
投票

你在这里有很多工作要做。 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
}

可能有一个更简单的解决方案,但这可行...

© www.soinside.com 2019 - 2024. All rights reserved.