Support » Plugin: WooCommerce » function of custom fields to the emails doesn’t works

  • Resolved Alessandra

    (@alexdra)


    Hey guys, I try to put into my function.php the code you said to use at this page to see custom field also into email. I replaced meta_key with mine but it doesn’t works anyways.
    I have seen looking on the net that it is also a problem of other users, can you tell me what is the solution and update the documentation?

    This is my function:

    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' => __( 'Codice fiscale' ),
            'value' => get_post_meta( $order->id, 'codicefiscale', true ),
        );
    
        $fields['partitaiva'] = array(
            'label' => __( 'P. IVA' ),
            'value' => get_post_meta( $order->id, 'partitaiva', true ),
        );
    
        $fields['indirizzopec'] = array(
            'label' => __( 'PEC' ),
            'value' => get_post_meta( $order->id, 'indirizzopec', true ),
        );
    
        $fields['codiceunivoco'] = array(
            'label' => __( 'Codice univoco' ),
            'value' => get_post_meta( $order->id, 'codiceunivoco', true ),
        );
    
        return $fields;
    }

    Thankyou very much,
    Alex

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Laurena Rehbein

    (@lrehbein)

    Automattic Happiness Engineer

    Hi Alex,

    I have tested the code you’ve linked to here, on my test site, and it is working well for me – the custom information is displayed in the emails. However, in my tests, I used it to display information that I had entered in the Custom Fields area of the order.

    I don’t immediately see any issues with your code, but perhaps it depends on the source of the meta information.

    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 1 year, 4 months ago by Alessandra.
    Plugin Support AW

    (@slash1andy)

    Automattic Happiness Engineer

    Hey there!

    Are the fields you are adding saving the meta into the Custom Fields in the order? That is where Laurena was pulling the info from, so I think the first step is to make sure your custom fields are saving with the order.

    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

    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘function of custom fields to the emails doesn’t works’ is closed to new replies.