I am trying to add additional fields to the checkout page. I have setup my ACF fields and can display them on the checkout page (via acf_form), but when I place the order, the values I input in the field does not carry over to the created order.
This is the code I am using to display and update the fields:
add_action( 'woocommerce_after_order_notes', 'my_acf_checkout_display' );
function my_acf_checkout_display() {
acf_form(array('form' => false,'fields' => array('inseam')));
}
add_action( 'woocommerce_checkout_update_order_meta','acf_update_field_at_checkout' );
function acf_update_field_at_checkout( $order_id ) {
// acf custom field id
$my_field = 'inseam';
$acf_form_value = $_POST['acf'][$my_field];
update_field($my_field,$acf_form_value,$order_id);
}
Here you can see how I set up the fields:
Here’s what my checkout page looks like:
And here is the "updated" field in the order (which remains empty):
When I hardcode the value to, say 12, then it works!
add_action( 'woocommerce_after_order_notes', 'my_acf_checkout_display' );
function my_acf_checkout_display() {
acf_form(array('form' => false,'fields' => array('inseam')));
}
add_action( 'woocommerce_checkout_update_order_meta','acf_update_field_at_checkout' );
function acf_update_field_at_checkout( $order_id ) {
// acf custom field id
$my_field = 'inseam';
$acf_form_value = 12;
//$acf_form_value = $_POST['acf'][$my_field];
update_field($my_field,$acf_form_value,$order_id);
}
Does anyone know why it’s not populating the entered value for inseam to the order?
Thank you very much in advance!
2
Answers
You don’t use the correct Field ID to be used with ACF.
So first in admin ACF Fields group click on "screen Options":
Then select to display the field "slug" and "Field Key" options.
Now you can get the correct "Field Key" to use in your code…
So here is the code I have tested, reproducing your settings (replace in the code the correct field Key for your "Inseam" field):
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Now the field is displayed in The Admin on the order page with the submitted value.
Handling multiple Advanced custom fields in a clean way
First, I have set 2 fields in ACF Field group settings:
Then Here is the code:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
The display in checkout page:
Thanks for the answer!
I have some text fields and 2 fields to upload files at the checkout.
These field’s values I’m saving to user’s meta.
But how to upload the files on checkout?
here’s my code: