WooCommerce 可编辑自定义结帐字段并显示在格式化地址中

WooCommerce editable custom checkout field and displayed in formatted address

我正在使用

添加强制送货 phone 到 woocommerce 结帐页面
add_filter( 'woocommerce_checkout_fields', 'add_shipping_phone_to_checkout_page' );

function add_shipping_phone_to_checkout_page( $fields ) {
   $fields['shipping']['shipping_phone'] = array(
      'label' => 'Phone',
      'required' => true,
      'class' => array( 'form-row-wide' ),
      'priority' => 25,
   );
   return $fields;
}

然后显示在管理订单面板

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'shipping_phone_checkout_display_in_order_panel' );

function shipping_phone_checkout_display_in_order_panel( $order ){
    echo '<p><b>Phone :</b> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}

最后打印在电子邮件中

add_action('woocommerce_email_customer_details','shipping_phone_display_in_order_email', 25, 4 );

function shipping_phone_display_in_order_email( $order, $sent_to_admin, $plain_text, $email ) {

    $output = '';
    $shipping_phone = get_post_meta( $order->id, '_shipping_phone', true );

    if ( !empty($shipping_phone) )
        $output = '<p><strong>' . __( "Phone:", "woocommerce" ) . '</strong> ' . $shipping_phone . '</p>';

    echo $output;

 }

一切正常。我想实现 2 个增强,但我做不到:

  1. 使自定义 phone 字段在管理面板中可编辑
  2. 在电子邮件中,将自定义 phone 字段值移至送货地址块

如有任何帮助,我们将不胜感激

您需要对代码进行一些更改...以下代码将显示运费 phone 字段:

  • 结帐
  • 我的帐户 > 地址 > 编辑送货地址
  • 管理员订单编辑页面

该代码还会将送货 phone 添加到电子邮件送货地址部分的格式化 显示 送货地址。

// display shipping phone in checkout and my account edit shipping address
add_filter( 'woocommerce_shipping_fields', 'add_shipping_phone_field' );
function add_shipping_phone_field( $fields ) {
   $fields['shipping_phone'] = array(
      'label' => __('Phone (Shipping)'),
      'required' => true,
      'class' => array( 'form-row-wide' ),
      'priority' => 25,
   );
   return $fields;
}

// Editable field on admin order edit pages inside edit shipping section
add_filter( 'woocommerce_admin_shipping_fields' , 'add_order_admin_edit_shipping_phone' );
function add_order_admin_edit_shipping_phone( $fields ) {
    // Include shipping phone as editable field
    $fields['phone'] = array( 'label' => __("Shipping phone"), 'show' => '0' );

    return $fields;
}

// Adding custom placeholder to woocommerce formatted address only on Backend
add_filter( 'woocommerce_localisation_address_formats', 'admin_localisation_address_formats', 50, 1 );
function admin_localisation_address_formats( $address_formats ){
    // Only in backend (Admin)
    if( is_admin() || ! is_wc_endpoint_url() ) {
        foreach( $address_formats as $country_code => $address_format ) {
            $address_formats[$country_code] .= "\n{phone}";
        }
    }
    return $address_formats;
}

// Custom placeholder replacement to woocommerce formatted address
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args  ) {
    $replacements['{phone}'] = ! empty($args['phone']) ? $args['phone'] : '';

    return $replacements;
}

// Add the shipping phone value to be displayed on email notifications under shipping address
add_filter( 'woocommerce_order_formatted_shipping_address', 'add_shipping_phone_to_formatted_shipping_address', 100, 2 );
function add_shipping_phone_to_formatted_shipping_address( $shipping_address, $order ) {
    global $pagenow, $post_type;

    // Not on admin order edit pages (as it's already displayed).
    if( ! ( $pagenow === 'post.php' && $post_type === 'shop_order' && isset($_GET['action']) && $_GET['action'] === 'edit' ) ) {
        // Include shipping phone on formatted shipping address
        $shipping_address['phone'] = $order->get_meta('_shipping_phone');
    }
    return $shipping_address;
}

// Remove double billing phone from email notifications (and admin) under billing address
add_filter( 'woocommerce_order_formatted_billing_address', 'remove_billing_phone_from_formatted_billing_address', 100, 2 );
function remove_billing_phone_from_formatted_billing_address( $billing_address, $order ) {
    unset($billing_address['phone']);
  
    return $billing_address;
}

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

For billing custom fields, you will replace the hooks:

  • woocommerce_shipping_fields by woocommerce_billing_fields
  • woocommerce_admin_shipping_fields by woocommerce_admin_billing_fields
  • woocommerce_order_formatted_shipping_address by woocommerce_order_formatted_billing_address
  • (don't use the last function).

对于前端端点:

在收到订单(谢谢)、订单付款和我的帐户/订单查看后,您将有 to override via your active theme the template order/order-details-customer.php

您将在第 52 行之后的 html 标记 <address> 内添加以下内容:

        <?php if ( $shipping_phone = $order->get_meta('_shipping_phone') ) : ?>
            <p class="woocommerce-customer-details--phone"><?php echo esc_html( $shipping_phone ); ?></p>
        <?php endif; ?>

在管理员端,运费 phone 显示并可编辑:


在查看订单、收到订单和电子邮件通知时,送货地址 phone 显示在送货地址部分的末尾: