Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

php - Order custom fields not displayed on WooCommerce email notifications

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...