Hi folks,
I have a small issue when trying on the woocommerce_checkout_update_order_meta hook to affiliate two user profiles.
The two profiles are generated during the process one by woo commerce ( client, main user ) and another one with wp_insert_user() ( secondary user ).
Then I have a acf field assign on the main user which should contain the id of the secondary user.
When I test it the field is empty. I tried several things like changing the priority of the action but no luck.
Is someone have a clue ?
Thanks !
// !JFW - WC Checkout Update Order Meta Hook in case of couple users to perform the second user creation and the association with the main user.
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta', 1, 2 );
function customise_checkout_field_update_order_meta($order_id)
{
if (!empty($_POST['jfw_tmp_couple_title'])) {
update_post_meta( $order_id, 'jfw_tmp_couple_title', sanitize_text_field( $_POST['jfw_tmp_couple_title'] ) );
}
if (!empty($_POST['jfw_tmp_couple_first_name'])) {
update_post_meta( $order_id, 'jfw_tmp_couple_first_name', sanitize_text_field( $_POST['jfw_tmp_couple_first_name'] ) );
}
if (!empty($_POST['jfw_tmp_couple_last_name'])) {
update_post_meta( $order_id, 'jfw_tmp_couple_last_name', sanitize_text_field( $_POST['jfw_tmp_couple_last_name'] ) );
}
if (!empty($_POST['jfw_tmp_couple_email'])) {
update_post_meta( $order_id, 'jfw_tmp_couple_email', sanitize_text_field( sanitize_email( $_POST['jfw_tmp_couple_email'] ) ) );
}
if (!empty($_POST['jfw_tmp_couple_birthday_date'])) {
update_post_meta( $order_id, 'jfw_tmp_couple_birthday_date', sanitize_text_field( $_POST['jfw_tmp_couple_birthday_date'] ) );
}
if (!empty($_POST['jfw_tmp_couple_phone'])) {
update_post_meta( $order_id, 'jfw_tmp_couple_phone', sanitize_text_field( $_POST['jfw_tmp_couple_phone'] ) );
}
if (!empty($_POST['jfw_tmp_couple_first_name'])) {
if (!empty($_POST['jfw_tmp_couple_last_name'])) {
if (!empty($_POST['jfw_tmp_couple_email'])) {
$couple_user_data = array(
'user_login' => strtolower( $_POST['jfw_tmp_couple_first_name'].$_POST['jfw_tmp_couple_last_name'] ),
'user_pass' => wp_generate_password ( 6, false ),
'user_email' => $_POST['jfw_tmp_couple_email'],
'display_name' => $_POST['jfw_tmp_couple_first_name'] . ' ' . $_POST['jfw_tmp_couple_last_name'],
'nickname' => strtolower( $_POST['jfw_tmp_couple_first_name'].$_POST['jfw_tmp_couple_last_name'] ),
'first_name' => $_POST['jfw_tmp_couple_first_name'],
'last_name' => $_POST['jfw_tmp_couple_last_name'],
'role' => 'subscriber',
);
$new_user = wp_insert_user( $couple_user_data );
if (!empty($_POST['jfw_tmp_couple_phone'])) {
update_user_meta( $new_user , 'billing_phone', $_POST['jfw_tmp_couple_phone'] );
}
if (!empty($_POST['jfw_tmp_couple_birthday_date'])) {
update_user_meta( $new_user , 'jfw_register_birthday_date', $_POST['jfw_tmp_couple_birthday_date'] );
}
if (!empty($_POST['jfw_tmp_couple_title'])) {
update_user_meta( $new_user , 'jfw_register_title', $_POST['jfw_tmp_couple_title'] );
}
$main_customer_id = wc_get_order( $order_id )->customer_id;
if ( function_exists( 'update_field' ) ){
update_field( 'field_5a68a6dda4b03', array( 'ID' => $news_user ), 'user_' . $main_customer_id );
}
wp_mail( 'xxx', 'debug', $new_user . ' ' . $main_customer_id );
}
}
}
}
Problem resolved,
I haded this after le update_field method :
if ( isset( $_POST['acf']['field_5a68a6dda4b03'] ) )
unset($_POST['acf']['field_5a68a6dda4b03'] );
And mostly I correct the false variable name $new_user and not $news_user.
So dumb !
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 ACF & ACF PRO 6.0.7 are now available.
— Advanced Custom Fields (@wp_acf) January 18, 2023
✨This release contains bug fixes and improvements while we continue to work on the next major release of ACF.https://t.co/wQgAOpwmUI
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.