0

Hi I have a question regardin custom checkout fields in woocommerce. I created a custom field in the checkout form and everything was working perfectly. The field contains a customer card number. I also managed to save the field value (once entered the first time) in the wp-usermeta, so that it doesn't come along only with the order, but it is saved together with teh customer details.

Now I would like to do the following. Once a registered customer returning to the shop goes to the checkout form, the new field (if not empyt) shows up automatically, instead of asking the customers to insert their card number everytime.

The original functions that I added to my child theme fucntions.php for showing the custom field in the checkout page was:

 /** * Add the field to the checkout */ add_action ('woocommerce_after_order_notes', 'checkout_w55_card_number' ); function checkout_w55_card_number( $checkout ) { echo '<div id="checkout_w55_card_number"><h2>' . __('N. tessera W55') . '</h2>'; woocommerce_form_field( 'w55_card_number', array( 'type' => 'text', 'class' => array('w55-card-number-class form-row-wide'), 'label' => __('Inserisci n. tessera W55'), 'placeholder' => __('Inserisci numero'), ), $checkout->get_value( 'w55_card_number' )); echo '</div>'; } 

I thus tried to re-arrange it in this way, but it doesn't work

add_action ('woocommerce_after_order_notes', 'checkout_w55_card_number' ); function checkout_w55_card_number( $checkout ) { //extracts the value of the w55_card_number field $w55_card = get_user_meta($user_id, 'w55_card_number', true); //condition is the w55_card_number field is empty or not if ( !empty($w55_card) ) : echo $w55_card; else : echo '<div id="checkout_w55_card_number"><h2>' . __('N. tessera W55') . '</h2>'; woocommerce_form_field( 'w55_card_number', array( 'type' => 'text', 'class' => array('w55-card-number-class form-row-wide'), 'label' => __('Inserisci n. tessera W55'), 'placeholder' => __('Inserisci numero'), ), $checkout->get_value( 'w55_card_number' )); echo '</div>'; endif; } 

Any suggestion?

1 Answer 1

1

Your user_id variable was not defined. You need to do the following:

add_action ('woocommerce_after_order_notes', 'checkout_w55_card_number' ); function checkout_w55_card_number( $checkout ) { $user_id = get_current_user_id(); $w55_card = ''; if ($user_id) { //extracts the value of the w55_card_number field $w55_card = get_user_meta($user_id, 'w55_card_number', true); } //condition is the w55_card_number field is empty or not if ( !empty($w55_card) ) : echo $w55_card; else : echo '<div id="checkout_w55_card_number"><h2>' . __('N. tessera W55') . '</h2>'; woocommerce_form_field( 'w55_card_number', array( 'type' => 'text', 'class' => array('w55-card-number-class form-row-wide'), 'label' => __('Inserisci n. tessera W55'), 'placeholder' => __('Inserisci numero'), ), $checkout->get_value( 'w55_card_number' )); echo '</div>'; endif; 

}

Sign up to request clarification or add additional context in comments.

1 Comment

Ohhh great!! Thanks @mirzap!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.