Custom fields not saved
-
Hello
I add the custom field “username_field” to the checkout and saving into order metadata with the following code:
<?php add_action( 'woocommerce_before_checkout_billing_form', 'add_username_field' ); function add_username_field( $cart ) { if (! function_exists( 'woocommerce_form_field' ) ) { return false; } ?> <div id="my_plugin"> <?php woocommerce_form_field( 'username_field', array( 'type' => 'text', 'class' => array(), 'label' => __( 'Username', 'myplugin' ), 'placeholder' => __( 'A valid username', 'myplugin' ), 'required' => true, ), $cart->get_value( 'username_field' ) ); ?> </div> <?php return true; } add_action( 'wt_woocommerce_checkout_order_created', 'save_username_to_order', 10, 2 ); function save_username_to_order( $order, $checkout_post ) { if ( empty( $order ) ) { return; } $id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; if ( ! isset( $checkout_post['username_field'] ) || empty( $checkout_post['username_field'] ) ) { return; } $user_name = sanitize_text_field( $checkout_post['username_field'] ); update_post_meta( $id, 'username_field', $user_name ); }
When someone use the smart button to pay with credit card one of three things happens:
1. The custom field is saved correctly
2. The custom field is not being saved
3. The custom field is saved into billing phone
Viewing 9 replies - 1 through 9 (of 9 total)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘Custom fields not saved’ is closed to new replies.