skip to Main Content

I wrote a little plugin that adds a couple of custom fields on checkout page. The fields are related to B2B buyers so they can input Company name, license number and address.

Everything pretty much works fine, except the values are not being displayed in order emails, most importantly, order completed email.

Here is the part of code I’m having trouble with

add_filter('woocommerce_email_order_meta_fields', 'mx_woocommerce_email_order_meta_fields');

function mx_woocommerce_email_order_meta_fields( $fields ) {

    if (isset($_POST['r1_checkbox']) && !empty($_POST['r1_checkbox'])) {
        echo '<h2>R1 račun za kupca</h2>';

        $fields[] = 'Ime tvrtke';
        $fields[] = 'Adresa tvrtke';
        $fields[] = 'OIB tvrtke';

        return $fields;
    }
}

Can’t figure out what is wrong. I also tried using $keys instead of $fields but that didn’t help.


UPDATE (full code):

/**
 * Let's add our new fields to the checkout
 */
add_action( 'woocommerce_after_checkout_billing_form', 'mx_custom_checkout_field' );

function mx_custom_checkout_field( $checkout ) {

    echo '<div id="mx_custom_checkout_field"><h3>' . __( 'Pravne osobe' ) . '</h3><p style="margin: 0 0 5px;">Trebate li R1 račun?</p>';

    woocommerce_form_field( 'r1_checkbox', array(
    'type'  => 'checkbox',
    'class' => array( 'r1-checkbox form-row-wide' ),
    'label' => __( 'Da' ),
    ), $checkout->get_value( 'r1_checkbox' ) );

    woocommerce_form_field( 'r1_ime_tvrtke', array(
   'type'  => 'text',
   'class' => array( 'r1-text form-row-wide' ),
   'label' => __( 'Ime tvrtke' ),
   'placeholder'   => _x('Upišite ime tvrtke', 'placeholder', 'woocommerce'),
   ), $checkout->get_value( 'r1_ime_tvrtke' ) );

    woocommerce_form_field( 'r1_adresa_tvrtke', array(
   'type'  => 'text',
   'class' => array( 'r1-text form-row-wide' ),
   'label' => __( 'Adresa tvrtke' ),
   'placeholder'   => _x('Upišite adresu tvrtke', 'placeholder', 'woocommerce'),
   ), $checkout->get_value( 'r1_adresa_tvrtke' ) );

    woocommerce_form_field( 'r1_oib_tvrtke', array(
  'type'  => 'text',
  'class' => array( 'r1-text form-row-wide' ),
  'label' => __( 'OIB tvrtke' ),
  'placeholder'   => _x('Upišite OIB tvrtke', 'placeholder', 'woocommerce'),
   ), $checkout->get_value( 'r1_oib_tvrtke' ) );

   echo '</div>';

}

/**
 * Let's update the order meta with fields values
 **/
add_action( 'woocommerce_checkout_update_order_meta', 'mx_oib_checkout_field_update_order_meta' );

function mx_oib_checkout_field_update_order_meta( $order_id ) {

   //check if $_POST has our custom fields and accordingly update meta for this order
   if ( $_POST['r1_checkbox'] ) {
   update_post_meta( $order_id, 'R1 račun', esc_attr( $_POST['r1_checkbox'] ) );
   }
   if ( $_POST['r1_ime_tvrtke'] ) {
   update_post_meta( $order_id, 'Ime tvrtke', esc_attr( $_POST['r1_ime_tvrtke'] ) );
   }
   if ( $_POST['r1_adresa_tvrtke'] ) {
   update_post_meta( $order_id, 'Adresa tvrtke', esc_attr( $_POST['r1_adresa_tvrtke'] ) );
   }
   if ( $_POST['r1_oib_tvrtke'] ) {
   update_post_meta( $order_id, 'OIB tvrtke', esc_attr( $_POST['r1_oib_tvrtke'] ) );
   }
}
/**
 * Let's display fields values on the Order edit page
 */
add_action( 'woocommerce_admin_order_data_after_billing_address', 'mx_oib_checkout_field_display_admin_order_meta', 10, 1 );

function mx_oib_checkout_field_display_admin_order_meta($order){

    $r1_checkbox = get_post_meta( $order->id, 'R1 račun', true );
    if( !empty( $r1_checkbox ) ) {

    echo '<h4>'.__('R1 račun').'</h4>';
    echo '<p><strong>'.__('Ime tvrtke').':</strong> ' . get_post_meta( $order->id, 'Ime tvrtke', true ) . '</p>';
    echo '<p><strong>'.__('Adresa tvrke').':</strong> ' . get_post_meta( $order->id, 'Adresa tvrtke', true ) . '</p>';
    echo '<p><strong>'.__('OIB tvrtke').':</strong> ' . get_post_meta( $order->id, 'OIB tvrtke', true ) . '</p>';
    }
}
/**
 * Let's display fields values on the Order details page
 */
add_action( 'woocommerce_order_details_after_order_table', 'mx_oib_field_display_cust_order_meta', 10, 1 );

function mx_oib_field_display_cust_order_meta($order){

    $r1_checkbox = get_post_meta( $order->id, 'R1 račun', true );
    if( !empty( $r1_checkbox ) ) {

    echo '<h2>'.__('Podaci za R1 račun').'</h2>';
    echo '<p><strong>'.__('Ime tvrtke').':</strong> ' . get_post_meta( $order->id, 'Ime tvrtke', true ) . '</p>';
    echo '<p><strong>'.__('Adresa tvrke').':</strong> ' . get_post_meta( $order->id, 'Adresa tvrtke', true ) . '</p>';
    echo '<p><strong>'.__('OIB tvrtke').':</strong> ' . get_post_meta( $order->id, 'OIB tvrtke', true ) . '</p>';
    }
}
/**
 * Let's add the new fields to order emails
 **/
add_filter('woocommerce_email_order_meta_keys', 'mx_woocommerce_email_order_meta_keys');

function mx_woocommerce_email_order_meta_keys( $keys ) {

    if ( $_POST['r1_checkbox'] ) {
    echo '<h2>R1 račun za kupca</h2>';

    $keys[] = 'Ime tvrtke';
    $keys[] = 'Adresa tvrtke';
    $keys[] = 'OIB tvrtke';

    return $keys;
    }
}
/**
 * Let's add the R1 column in order administration
 **/

add_filter( 'manage_edit-shop_order_columns', 'mx_r1_order_column', 20 );
function mx_r1_order_column( $columns ) {
    $offset = 9;
    $updated_columns = array_slice( $columns, 0, $offset, true) +
    array( 'r1_checkbox' => esc_html__( 'R1 račun', 'woocommerce' ) ) +
    array_slice($columns, $offset, NULL, true);
    return $updated_columns;
}

// Populate R1 column
add_action( 'manage_shop_order_posts_custom_column', 'mx_r1_order_column_values', 2 );
function mx_r1_order_column_values( $column ) {
    global $post;

    if ( $column == 'r1_checkbox' ) {
        $r1_checkbox = get_post_meta( $post->ID, 'R1 račun', true );
        if ( $r1_checkbox > 0 )
            print ("DA");
        else print '-';
    }
}

Note that r1_checkbox is just a checkbox that toggles 3 fields under it.

2

Answers


  1. Your code has some little mistakes here and there, I’ve revised it

    I also changed a deprecated hook:

    • woocommerce_email_order_meta_keys by woocommerce_email_order_meta_fields

    Replaced by a much appropriated hook:

    • woocommerce_checkout_update_order_meta by
      woocommerce_checkout_create_order
    /**
     * Let's add our new fields to the checkout
     */
    function mx_custom_checkout_field( $checkout ) {
        
        echo '<div id="mx_custom_checkout_field"><h3>' . __( 'Pravne osobe' ) . '</h3><p style="margin: 0 0 5px;">Trebate li R1 račun?</p>';
    
        woocommerce_form_field( 'r1_checkbox', array(
            'type'  => 'checkbox',
            'class' => array( 'r1-checkbox form-row-wide' ),
            'label' => __( 'Da' ),
        ), $checkout->get_value( 'r1_checkbox' ) );
    
        woocommerce_form_field( 'r1_ime_tvrtke', array(
            'type'  => 'text',
            'class' => array( 'r1-text form-row-wide' ),
            'label' => __( 'Ime tvrtke' ),
            'placeholder'   => _x('Upišite ime tvrtke', 'placeholder', 'woocommerce'),
        ), $checkout->get_value( 'r1_ime_tvrtke' ) );
    
        woocommerce_form_field( 'r1_adresa_tvrtke', array(
            'type'  => 'text',
            'class' => array( 'r1-text form-row-wide' ),
            'label' => __( 'Adresa tvrtke' ),
            'placeholder'   => _x('Upišite adresu tvrtke', 'placeholder', 'woocommerce'),
        ), $checkout->get_value( 'r1_adresa_tvrtke' ) );
    
        woocommerce_form_field( 'r1_oib_tvrtke', array(
            'type'  => 'text',
            'class' => array( 'r1-text form-row-wide' ),
            'label' => __( 'OIB tvrtke' ),
            'placeholder'   => _x('Upišite OIB tvrtke', 'placeholder', 'woocommerce'),
        ), $checkout->get_value( 'r1_oib_tvrtke' ) );
    
        echo '</div>';                           
    }
    add_filter( 'woocommerce_after_checkout_billing_form' , 'mx_custom_checkout_field', 10, 1 );
    
    /**
     * Let's save
     **/
    function mx_oib_checkout_field_create_order( $order, $data ) {
        //check if $_POST has our custom fields and accordingly update meta for this order
        if ( isset($_POST['r1_checkbox']) && ! empty($_POST['r1_checkbox']) ) {
            $order->update_meta_data( 'r1_checkbox', sanitize_text_field( $_POST['r1_checkbox'] ) );
        }
        
        if ( isset($_POST['r1_ime_tvrtke']) && ! empty($_POST['r1_ime_tvrtke']) ) {
            $order->update_meta_data( 'r1_ime_tvrtke', sanitize_text_field( $_POST['r1_ime_tvrtke'] ) );
        } 
        
        if ( isset($_POST['r1_adresa_tvrtke']) && ! empty($_POST['r1_adresa_tvrtke']) ) {
            $order->update_meta_data( 'r1_adresa_tvrtke', sanitize_text_field( $_POST['r1_adresa_tvrtke'] ) );
        } 
        
        if ( isset($_POST['r1_oib_tvrtke']) && ! empty($_POST['r1_oib_tvrtke']) ) {
            $order->update_meta_data( 'r1_oib_tvrtke', sanitize_text_field( $_POST['r1_oib_tvrtke'] ) );
        } 
    }
    add_action( 'woocommerce_checkout_create_order', 'mx_oib_checkout_field_create_order', 10, 2 );
    
    /**
     * Let's display fields values on the Order edit page after billing adress
     */
    function mx_oib_checkout_field_display_admin_order_meta( $order ) {
        $r1_checkbox = $order->get_meta('r1_checkbox');
        
        if( $r1_checkbox ) {
            echo '<h4>'.__('R1 račun').'</h4>';
            echo '<p><strong>'.__('Ime tvrtke').':</strong> ' . $order->get_meta('r1_ime_tvrtke') . '</p>'; 
            echo '<p><strong>'.__('Adresa tvrke').':</strong> ' . $order->get_meta('r1_adresa_tvrtke') . '</p>';        
            echo '<p><strong>'.__('OIB tvrtke').':</strong> ' . $order->get_meta('r1_oib_tvrtke') . '</p>'; 
        }
    }
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'mx_oib_checkout_field_display_admin_order_meta', 10, 1 );
    
    /**
     * Let's display fields values on the Order details page
     */
    function mx_oib_field_display_cust_order_meta($order){
        $r1_checkbox = $order->get_meta('r1_checkbox');
        
        if( $r1_checkbox ) {
            echo '<h4>'.__('R1 račun').'</h4>';
            echo '<p><strong>'.__('Ime tvrtke').':</strong> ' . $order->get_meta('r1_ime_tvrtke') . '</p>'; 
            echo '<p><strong>'.__('Adresa tvrke').':</strong> ' . $order->get_meta('r1_adresa_tvrtke') . '</p>';        
            echo '<p><strong>'.__('OIB tvrtke').':</strong> ' . $order->get_meta('r1_oib_tvrtke') . '</p>'; 
        }
    }
    add_action( 'woocommerce_order_details_after_order_table', 'mx_oib_field_display_cust_order_meta', 10, 1 );
    
    /**
     * Let's add the new fields to order emails
     **/
    function mx_oib_checkout_field_meta_fields( $fields, $sent_to_admin, $order  ) {
    
        if( ! $order->get_meta( 'r1_checkbox' ) )
            return $fields;
    
        echo '<h4>'.__( 'R1 račun', 'woocommerce' ).'</h2>';
    
        $fields[] = array(
            'label' => __( 'Ime tvrtke', 'woocommerce' ),
            'value' => $order->get_meta( 'r1_ime_tvrtke' )
        );
        
        $fields[] = array(
            'label' => __( 'Adresa tvrke', 'woocommerce' ),
            'value' => $order->get_meta( 'r1_adresa_tvrtke' )
        );
    
        $fields[] = array(
            'label' => __( 'OIB tvrtke', 'woocommerce' ),
            'value' => $order->get_meta( 'r1_oib_tvrtke' )
        );
    
        return $fields;
    }
    add_filter( 'woocommerce_email_order_meta_fields', 'mx_oib_checkout_field_meta_fields', 20, 3 );
    
    /**
     * Let's add the R1 column in order administration
     **/
    function mx_r1_order_column( $columns ) {
        /*
        $offset = 9;
        $updated_columns = array_slice( $columns, 0, $offset, true) +
        array( 'r1_checkbox' => esc_html__( 'R1 račun', 'woocommerce' ) ) +
        array_slice($columns, $offset, NULL, true);
        */
        
        $columns['r1_checkbox'] = __('R1 račun', 'woocommerce' );
        
        return $columns;
    }
    add_filter( 'manage_edit-shop_order_columns', 'mx_r1_order_column', 10, 1 );
    
    // Populate R1 column
    function mx_r1_order_column_values( $column, $post_id ) {
        if ( $column == 'r1_checkbox' ) {
            $r1_checkbox = get_post_meta( $post_id, 'r1_checkbox', true );
            
            if ( $r1_checkbox ) {
                echo 'DA';
            } else {
                echo '-';
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column', 'mx_r1_order_column_values', 10, 2 );
    
    Login or Signup to reply.
  2. There are some mistakes in your function code. To make it work, use the following:

    add_filter( 'woocommerce_email_order_meta_fields', 'filter_email_order_meta_fields_callback', 10, 3 );
    function filter_email_order_meta_fields_callback( $fields, $send_to_admin, $order ) {
        if ( $order->get_meta('R1 račun') ) {
            echo '<h2>R1 račun za kupca</h2>';
    
            // Defined array of meta keys (labels)
            $meta_keys = ['Ime tvrtke', 'Adresa tvrtke', 'OIB tvrtke'];
    
            // Loop though meta keys array to set the fields
            foreach( $meta_keys as $meta_key ){
                if( $meta_value = $order->get_meta($meta_key) ){
                    $fields[] = array(
                        'label' => $meta_key,
                        'value' => $meta_value,
                    );
                }
            }
        }
        return $fields;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    Note: You need to remove the following, as it’s deprecated and not needed anymore:

    add_filter('woocommerce_email_order_meta_keys', 'mx_woocommerce_email_order_meta_keys');
    
    function mx_woocommerce_email_order_meta_keys( $keys ) {
    
        if ( $_POST['r1_checkbox'] ) {
            echo '<h2>R1 račun za kupca</h2>';
    
            $keys[] = 'Ime tvrtke';
            $keys[] = 'Adresa tvrtke';
            $keys[] = 'OIB tvrtke';
    
            return $keys;
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search