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
Your code has some little mistakes here and there, I’ve revised it
I also changed a deprecated hook:
bywoocommerce_email_order_meta_keys
woocommerce_email_order_meta_fields
Replaced by a much appropriated hook:
bywoocommerce_checkout_update_order_meta
woocommerce_checkout_create_order
There are some mistakes in your function code. To make it work, use the following:
Code goes in functions.php file of your active child theme (or active theme). Tested and works.