在 WooCommerce 管理订单编辑页面中显示账单地址之间的自定义字段

Display custom field between billing address in WooCommerce admin order edit pages

我目前正在尝试将自定义字段添加到默认地址字段(名字、姓氏等...)

应使用该字段为客户设置称呼。

为此,我使用了以下过滤器:

add_filter( 'woocommerce_default_address_fields', 'custom_woocommerce_address_fields' );

function custom_woocommerce_address_fields($fields) {

    $fields['salutation'] = array(
        'label' => __('Anrede', 'woocommerce'), // Add custom field label
        'placeholder' => 'CA12345678', // Add custom field placeholder
        'required' => true, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'select', // add field type
        'options' => array(
            'Herr' => 'Herr',
            'Frau' => 'Frau',
            'Firma' => 'Firma'
        ),
        'priority' => 1, // add priority
        'class' => array('billing-salutation-input')// add class name
    );

    return $fields;
}

保存自定义字段:

add_action( 'woocommerce_checkout_update_order_meta', 'save_new_checkout_field' );
  
function save_new_checkout_field( $order_id ) { 
    if ( $_POST['billing_salutation'] ) update_post_meta( $order_id, '_salutation', esc_attr( $_POST['billing_salutation'] ) );
}

问题:

当我查看订单详细信息时,新字段不会显示在默认地址字段下。

所以我使用以下代码来显示新数据。

add_action( 'woocommerce_admin_order_data_after_billing_address', 'show_new_checkout_field_order', 10, 1 );
   
function show_new_checkout_field_order( $order ) {    
   $order_id = $order->get_id();
   
   if ( get_post_meta( $order_id, '_salutation', true ) ) echo '<p><strong>Anrede:</strong> ' . get_post_meta( $order_id, '_salutation', true ) . '</p>';
}

但是,该字段总是显示在底部,我找不到将字段带到所需位置的方法(图片)

我是否必须使用不同的挂钩才能在所需位置(账单明细)显示数据?

或者我是否必须编辑特定的 woocommerce 模板?

输出为字符串,使用<br/>分隔订单账单地址详情

所以有两种可能的观点:

  • company_name<br/>Firstname Lastname<br/>street<br/>1<br/>1000 Location
  • Firstname Lastname<br/>street<br/>1<br/>1000 Location

您可以采用多种可能性来回答您的问题,但最简单的方法是通过 woocommerce_order_get_formatted_billing_address 过滤器挂钩编辑字符串。

注意:我们要检查是否真的有账单公司, 并据此调整我们的结果。

注意:根据您的需要调整$order->get_meta( '_your_meta_key' );

所以你得到:

function str_replace_first( $search, $replace, $subject ) {
    $pos = strpos( $subject, $search );
    if ( $pos !== false ) {
        return substr_replace( $subject, $replace, $pos, strlen( $search ) );
    }
    return $subject;
}
 
/**
 * Filter orders formatterd billing address.
 *
 * @since 3.8.0
 * @param string   $address     Formatted billing address string.
 * @param array    $raw_address Raw billing address.
 * @param WC_Order $order       Order data. @since 3.9.0
 */
function filter_woocommerce_order_get_formatted_billing_address( $address, $raw_address, $order ) { 
    // Get meta
    $value = $order->get_meta( '_your_meta_key' );
    
    // When empty, return 
    if ( empty ( $value ) ) return $address;  
    
    // Get billing company
    $billing_company = $order->get_billing_company();
    
    // Not empty
    if ( ! empty ( $billing_company ) ) {
        $address = str_replace_first( '<br/>', '<br/>' . $value . '<br/>', $address );
    } else {
        $address = $value . '<br/>' . $address;
    }       

    return $address;
}
add_filter( 'woocommerce_order_get_formatted_billing_address', 'filter_woocommerce_order_get_formatted_billing_address', 10, 3 );

结果:

  • company_name<br/>meta_value<br/>Firstname Lastname<br/>street<br/>1<br/>1000 Location
  • meta_value<br/>Firstname Lastname<br/>street<br/>1<br/>1000 Location

在这个答案中使用:

绝对可行!您可以 append 它到现有的 billing_address_fields 数据。您只需要使用正确的钩子自定义您的数据!

因此您使用了 woocommerce_default_address_fields 过滤器挂钩 append $fields['salutation'] 到默认数据。在管理屏幕上,您需要执行相同的操作,但使用不同的挂钩。

这是我们要做的事情:

  1. 我们将使用 woocommerce_localisation_address_formats 过滤器挂钩来定义自定义数据的占位符。
  2. 然后我们将尝试使用 woocommerce_formatted_address_replacements 过滤器挂钩添加占位符的替代品。
  3. 最后,我们将获取自定义数据并填充我们使用 woocommerce_order_formatted_billing_address 过滤器挂钩定义的占位符。

1- woocommerce_localisation_address_formats

add_filter('woocommerce_localisation_address_formats', 'admin_customized_localisation_address_formats', 99);

function admin_customized_localisation_address_formats($billing_address_formats)
{
  if (is_admin()) {
    foreach($billing_address_formats as $key => $value) {
      $billing_address_formats[$key] .= "\n{salutation}";
    }
  }
  return $billing_address_formats;
}

2- woocommerce_formatted_address_replacements

add_filter('woocommerce_formatted_address_replacements', 'adding_customized_replacements', 10, 2);

function adding_customized_replacements($replacements, $args)
{
  $replacements["{salutation}"] = !empty($args["salutation"])  ? $args["salutation"] : "";
  return $replacements;
}

3- woocommerce_order_formatted_billing_address

add_filter('woocommerce_order_formatted_billing_address', 'display_customized_order_address_fields_on_admin', 10, 2);

function display_customized_order_address_fields_on_admin($address, $order)
{
  $order_id = $order->get_id();

  $address["salutation"] = get_post_meta($order_id, '_salutation', true) ? 'Anrede: ' . get_post_meta($order_id, '_salutation', true) : "No salutation data";

  return $address;
}

这将输出: