Forum Replies Created

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey there, I found what was wrong. To send the email it was not the slug (example ‘codicefiscale’) I have to specify, but the field (example ‘Codice Fiscale’).
    To use the custom field I wrote before, the code to sent the email is the one below:

    /**
     * Add a custom field (in an order) to the emails
     */
    add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
    
    function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
        $fields['codicefiscale'] = array(
            'label' => __( <strong>'Codice Fiscale'</strong> ),
            'value' => get_post_meta( $order->id, 'Codice Fiscale', true ),
        );
        $fields['partitaiva'] = array(
            'label' => __( <strong>'P. IVA'</strong> ),
            'value' => get_post_meta( $order->id, 'P. IVA', true ),
        );
        return $fields;
    }
    

    Cheers,
    Alex

    Hi Andrew,

    this is what I do into my function.php:
    First of all, I create fields with the function:

    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
    function my_custom_checkout_field( $checkout ) {
    echo '<div><h2>' . __('Dati aggiuntivi:') . '</h2>';
    woocommerce_form_field( 'codicefiscale', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Codice Fiscale'),
            'placeholder'   => __(' '),
            'required'    => true,
            ), $checkout->get_value( 'codicefiscale' ));
    woocommerce_form_field( 'partitaiva', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('P. IVA'),
            'placeholder'   => __(' '),
            'required'    => true,
            ), $checkout->get_value( 'partitaiva' ));
          echo '<p>' . __('Inserire l\'Indirizzo PEC:') . '</p>';
    echo '</div>';
    }

    than I 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['codicefiscale'] ) ) {
       update_post_meta( $order_id, 'Codice Fiscale', sanitize_text_field( $_POST['codicefiscale'] ) );
    }
      if ( ! empty( $_POST['partitaiva'] ) ) {
            update_post_meta( $order_id, 'P. IVA', sanitize_text_field( $_POST['partitaiva'] ) );
      }
    }

    and in the end I 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>'.__('Codice Fiscale').':</strong> ' . get_post_meta( $order->id, 'Codice Fiscale', true ) . '</p>';
        echo '<p><strong>'.__('P. IVA').':</strong> ' . get_post_meta( $order->id, 'P. IVA', true ) . '</p>';
    }

    Do I have to enter anything else to make the function for sending data via mail work?
    Thankyou very much

    Hi Laurena,

    I used this function to receive the custom fields added to the check-out form. Are the fields you added into the checkout forms?

    many thanks

    • This reply was modified 4 months, 2 weeks ago by Alessandra.

    I believe that the images are recovered because in the <header> I specified opengraph’tags. Thankyou very much for the answer.

Viewing 4 replies - 1 through 4 (of 4 total)