Support

Account

Home Forums Front-end Issues Frontend form without submit

Solving

Frontend form without submit

    • jarvis

    • July 26, 2017 at 6:41 pm

    Hi All,

    I wonder if someone can help!

    I’m looking to create a frontend form that can be completed but won’t include a submit button

    Basically, I’m looking to add some custom meta fields on a WooCommerce order, so once the order is placed, the user is created and the additional ACF fields are copied into the user

    Therefore, I want to exclude the ACF submit button as I will use a WooCommerce action, something like:

    
    add_action( 'woocommerce_thankyou', 'update_usermeta_from_acf');
     
    function update_usermeta_from_acf( $order_id ) {
         
        $order = new WC_Order( $order_id );
        $user_id = $order->user_id;
    
    	$user_meta_fields = array ( 'acf_field_1', 'acf_field_2', 'acf_field_3', 'acf_field_4', 'acf_field_5'  );
    	foreach($user_meta_fields as $user_meta_field){
    		update_user_meta($user_id, $user_meta_field, $posted[$user_meta_field]);
    	}
     
    }
    

    I may have overlooked the obvious and any help is much appreciated!

    Thanks

  • If you can modify the code where the WC form is displayed you can use acf_form() with the form option set to false. You will also not be able to use the WC hook and you’ll need to create an acf/pre_save_post action https://www.advancedcustomfields.com/resources/acf-pre_save_post/ to do the work otherwise ACF will go ahead and save the values to the order no matter what you do in the WC filter, and all you’ll really need to do in the filter (i think, don’t hold me to this) is to return the ID for the user like.

    
    // this is just a guess
    $post_id = 'user_'.$user_id;
    return $post_id;
    
    • jarvis

    • July 27, 2017 at 5:30 pm

    Thanks @john huebner

    I did try that but in the end had to leave this option. Once the main WooCommerce form was triggered, it would then come up with a leave this page error, I assume it’s because I had the ACF form as well

    Many thanks for your reply though

  • I gather from this that WC is submitting it’s form through AJAX. In that case I would add my own form fields to the WC form and then use the acf update functions like https://www.advancedcustomfields.com/resources/update_field/ to manually update the ACF user fields myself. When using these functions you should use the field key and not the field name to do the updates. You could also use update_user_meta() like in your example but you need to update the ACF field key reference in addition to the meta value, but it would be easier to use update_field using the field keys.

  • What about using 'form' => false in your call to acf_form so that ACF renders the fields, but doesn’t process them?

    • jarvis

    • July 27, 2017 at 10:20 pm

    Thanks Dave,

    Yep, that’s what I had. Am assuming it was the reason for the pop up

    At John, so instead of using the form fields,ref by key. Shall I still try form=>false as well?

  • I’m not completely sure how WC works in this case, but no, I don’t think acf_form() will help you.

    Guessing that this is what’s happening

    1. Submit form
    2. WC does an AJAX request to submit the form
    3. WC redirects

    ACF fields have not been submitted yet, ACF does not run on the AJAX request, but if you use acf_form() it is still loading everything that it would normally load for field validation and detecting changes.

    In order to use acf_form() you would need to:

    1. Add an action to all form fields and the form submit action that would clear the flag that tells ACF that something has been changed – I don’t know how to do this
    2. You would need to call acf_form_head() in your filter – This is a guess, I don’t know if this will work
    3. You would still need an acf/pre_save_post filter to cause ACF to save the values to the user instead of $order_id

    There are a lot of pieces here and some of what needs to be done is unknown. For me, it would just be easier to hand code the form fields rather than use ACF form and then update the user ACF fields with those values using update_field(). The amount of work and code would be far less, not to mention the hours of testing that would be involved and possibly completely failing to get it to work since I’m not certain that it can be done.

    • jarvis

    • July 27, 2017 at 11:30 pm

    Thanks John,

    The route I went with (albeit clunky):
    1) Add extra fields to the checkout process (custom meta stored to order post id)
    2) Setup ACF fields and add them to the user (i’m using a role as a rule)
    3) Add a function that checks when we hit the Woo Order thank you page
    4) Use get_post_meta( $order_id, 'custom_field', $single = true); to get the field added at the checkout
    5) Use update_user_meta( $user_id, 'custom_field', $company_name ); to move the info to the user

    Whilst it’s not ideal it does achieve what I need. It also means I’ve a hard copy (so to speak) of the additional data submitted by the customer as it’s part of the order

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

You must be logged in to reply to this topic.

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.