Hi,
I want to add custom fields for customers registration on woocommerce : they must give height and weight to buy a bike.
I created 2 fields and display them with this location :
'location' => array (
array (
array (
'param' => 'user_form',
'operator' => '==',
'value' => 'all',
),
array (
'param' => 'user_role',
'operator' => '==',
'value' => 'customer',
),
),
),
If I go to account to directly create an account, I can see these fields (quite strange for me because I thought that I needed to code something but…). The problem is that they don’t appear on the order form (checkout must be the right word…) where it is possible to create an account while we order a product.
How can I do this ?
Hi @wax
I’m afraid WooCommerce has a custom registration form on the checkout page. To modify it, please take a look at this page: https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/. To update the custom field value in the database, you can use the update_field()
function. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/update_field/.
I hope this helps. If you have any other questions, feel free to ask me again 🙂
Thanks. It looks like it is simpler to do all the thing with woocommerce functions ?
I found this yesterday :
https://support.woothemes.com/hc/en-us/articles/203182373-How-to-add-custom-fields-in-user-registration-on-the-My-Account-page
and I wonder if it is not easier for me to add custom fields like this than with acf (only for registration custom fields).
Because what you say to me is that I need to “marry” acf code & woocommerce code, isn’t it ?
Hi @wax
I believe the article you gave me is used on the my-account page, not the checkout page. If you want to add it on the checkout page, I believe the link I gave you is the proper one. In the order process, you can update the field by using the update_field()
function. Unfortunately, I’m not sure which hook you need to use. In this case, could you please ask the WooCommerce support instead?
Thanks 🙂
Sorry, my english is too bad : I wanted to say “thanks because u have helped me” and not “please help me” lol
I’ll code it. I’ll just take a lot of time to do it, but I will get there 🙂
I’m going to follow your advices.
Well, with you advices, it seems to work.
If someone is interested :
/**
* Add custom fields to user / checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="bv_custom_checkout_field"><h2>Measurements</h2>';
/* Weight */
woocommerce_form_field( 'weight_customer', array(
'type' => 'text',
'class' => array('my-class form-row-wide'),
'label' => __('Your weight'),
'placeholder' => __('Your weight'),
), get_user_meta( get_current_user_id(),'weight_customer' , true ) );
echo '</div>';
}
/**
* Verification
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check
if ( ! $_POST['weight_customer'] )
wc_add_notice( __( 'Do not forget weight.' ), 'error' );
}
* Update field
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['weight_customer'] ) ) {
update_user_meta( get_current_user_id(), 'weight_customer', sanitize_text_field( $_POST['weight_customer'], '' ));
}
}
I just don’t know how to tell him that it is a number (the weight) but it works.
To have numbers instead of text :
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
),
Now, gonna put all of this to trash cause what I really need is to have my custom field on each bought product : if I want to buy a bike for an adult and a bike for a child, I will have to give two different weights… o_O
Hi @wax
You can always create a field group with a repeater field in it. Then, you can add the field group to the order post type and use acf_form()
instead of woocommerce_form_field()
with ‘form’ option set to false in the checkout form. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/acf_form/. After that, just update the repeater values by using the update_field()
function.
You can get the posted repeater value like this:
$repeater_values = $_POST['acf']['field_1234567890abc'];
Where ‘field_1234567890abc’ is the field key.
I hope this makes sense 🙂
The topic ‘Custom fields for registration woocommerce’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.