• i’ve added a few custom fiels to my checkout page, but they don’t show in the order page after being filled.

    what am i doing wrong ?

    /**
    * Add the field to the checkout
    */
    addaction( 'woocommerceafterordernotes', 'mycustomcheckout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    
    echo '<div id="my_custom_checkout_field"><h3>' . __('Informations Passager(s)') . '</h3>';
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-first'),
    'label' => __('Passager 1'),
    'placeholder' => __('Prénom'),
    'required'  => true,
    ), $checkout->get_value( 'my_field_name' ));
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-last'),
    'placeholder' => __('Nom'),
    'label' => __('(interlocuteur principal)'),
    ), $checkout->get_value( 'my_field_name' ));
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-wide'),
    'label' => __(' '),
    'placeholder' => __('Numéro de passeport / CI'),
    ), $checkout->get_value( 'my_field_name' ));
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-first'),
    'label' => __('Passager 2'),
    'placeholder' => __('Prénom'),
    'required'  => false,
    ), $checkout->get_value( 'my_field_name' ));
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-last'),
    'placeholder' => __('Nom'),
    'label' => __('(si applicable)'),
    ), $checkout->get_value( 'my_field_name' ));
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-wide'),
    'label' => __(' '),
    'placeholder' => __('Numéro de passeport / CI'),
    ), $checkout->get_value( 'my_field_name' ));
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-first'),
    'label' => __('Passager 3'),
    'placeholder' => __('Prénom'),
    'required'  => false,
    ), $checkout->get_value( 'my_field_name' ));
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-last'),
    'placeholder' => __('Nom'),
    'label' => __('(si applicable)'),
    ), $checkout->get_value( 'my_field_name' ));
    
    woocommerce_form_field( 'my_field_name', array(
    'type' => 'text',
    'class' => array('my-field-class form-row-wide'),
    'label' => __(' '),
    'placeholder' => __('Numéro de passeport / CI'),
    ), $checkout->get_value( 'my_field_name' ));
    
    echo '</div>';
    
    }
    
    /**
    * Process the checkout
    */
    addaction('woocommercecheckoutprocess', 'mycustomcheckoutfield_process');
    
    function mycustomcheckoutfieldprocess() {
    // Check if set, if its not set add an error.
    if ( ! $POST['myfieldname'] )
    wcaddnotice( _( 'Merci de compléter ces champs.' ), 'error' );
    }
    
    /**
    * Update the order meta with field value
    */
    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['my_field_name'] ) ) {
    update_post_meta( $order_id, 'Informations Passager(s)', sanitize_text_field( $_POST['my_field_name'] ) );
    }
    }
    
    /**
    * Display field value on the order edit page
    */
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_custom_checkout_field_display_admin_order_meta($order){
    echo '<p><strong>'.__('Informations Passager(s)').':</strong> ' . get_post_meta( $order->id, 'Informations Passager(s)', true ) . '</p>';

    Thanks in advance 🙂

    https://wordpress.org/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • “addaction” should be “add_action”. Also, there are hook names without underscores.
    Please make sure they are all well written.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom fields in Checkout not showing in Order page’ is closed to new replies.