订单自定义字段未显示在 WooCommerce 电子邮件通知中

Order custom fields not displayed on WooCommerce email notifications

我写了一个小插件,在结帐页面上添加了几个自定义字段。这些字段与 B2B 买家相关,因此他们可以输入公司名称、许可证号和地址。

一切都很好,除了订单电子邮件中没有显示值,最重要的是,订单完成电子邮件中没有显示。

这是我遇到问题的部分代码

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

不知道哪里出了问题。我也尝试使用 $keys 而不是 $fields 但这没有帮助。


更新(完整代码)

/**
 * 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 '-';
    }
}

请注意 r1_checkbox 只是一个复选框,可以切换其下的 3 个字段。

你的代码有些小错误,我已经修改了

我还更改了一个已弃用的钩子:

  • woocommerce_email_order_meta_keys 来自 woocommerce_email_order_meta_fields

替换为一个非常合适的钩子:

  • woocommerce_checkout_update_order_meta 通过 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 );

您的功能代码中有一些错误。要使其工作,请使用以下内容:

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

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

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