Support

Account

Home Forums ACF PRO Append Fields to WooCommerce Register Form

Solving

Append Fields to WooCommerce Register Form

  • I’ve managed to attach an acf_form to the WooCommerce My Account page where they can add or update various details like telephone number, Facebook URL, etc and the data is saving correctly, updating the user meta. I’d also like to include those same fields in the register form. I’ve got as far as actually attaching the acf_form to the register form but I’m way off this handling the data correctly.

    I can’t set a post_id option for the form because the data needs to be attributed to the new user, not yet created. If I set post_id => ‘new_post’ then I get a new empty post, not a user. If I set post_id => false then the form doesn’t render.

    I wonder if it’s even possible to attach the acf_form data to whatever function is creating the new user without having to recreate the entire form and create the new, saving the user data myself. Any pointers towards a posible solution will be gratefully received.

  • I don’t think that there is any way to do this, but to be honest, I don’t know.

    You would somehow need to capture the user ID that was just created and before ACF tries to save the values.

    I don’t know if any of this will work.

    use "new_user" for the post ID in your form.

    https://www.advancedcustomfields.com/resources/acf-pre_save_post/

    
    // i'm using a class here so I can store a value in one action/filter
    // and retrieve it in a different action/filter
    // you could also do this using a global variable
    class my_custom_acf_save_user_form {
      
      private $user_id = false; // will store the user id just saved
    
      public function __construct() {
        add_action('user_register', array($this, 'user_register', 10, 1);
        add_filter('acf/pre_save_post', array($this, 'pre_save_post', 10, 1);
      }  // end __construct
    
      public function user_register($user_id) {
        // capture the id of the user just created
        $this->user_id = $user_id;
      } // end user_register
    
      public function pre_save_post($post_id) {
        if ($post_id == 'new_user' && $this->user_id) {
          $post_id = 'user_'.$this->user_id;
        }
        return $post_id;
      } // end pre_save_post
    
    } // end class
    
    new my_custom_acf_save_user_form();
    
  • This reply has been marked as private.
  • You would put the class in your functions.php file. It will instantiate when the file is loaded and add the needed action and filter will be added to WP.

    I use a class because we must get the new user ID with one filter and then use it in another filter. To do this you have 2 choices, create a class that can store values independently or use global variables, and I’m not a fan of global variables.

    And as I said in my last post, I am not sure that it will work, it’s all just a guess.

    If it does work, the method user_register() will run when a new user is registered, storing the new user id.

    Then the method pre_save_post() will run before ACF saves the post and we set the correct user ID for that user so ACF stores the values in the right place.

    This all hinges on the hook user_register firing before acf/pre_save_post

  • Thanks again for your help on this, John. I didn’t get it working in the end and instead reverted to using WooCommerce hooks to append fields to the registration form.

    I’ve not ruled out revisiting your proposed solution at a later date though as I’m using an ACF form on the user’s account page and would ideally like to be using the same one on the register form, rather than maintaining two sets of fields. If I do eventually get it to work I’ll update this thread with my results.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.